[PEAK] Re: dispatch rules based of the current scope.

Phillip J. Eby pje at telecommunity.com
Sun Mar 19 12:13:31 EST 2006


At 03:57 PM 3/19/2006 +0100, Elvelind Grandin wrote:
>Hi.
>
>One thing that supriced me in rulesdispatch was this.
>
>foo = generic()(foo)
>a = 1
>foo.when("a ==1")(print1)
>foo.when("a ==2")(print2)
>
>a = 2
>foo()
> >> 1
>
>is there any clever way of doing this besides putting a into the
>signature of the foo function?

Yes; make it a zero argument callable, e.g. "lambda: a".  Zero-argument 
callables are assumed to be dynamic; all other expressions that *can* be 
computed statically, *will* be.


>For my turbogears decorator that uses ruledispath I would like to
>allow an custom_rule argument. but that rule need to be evaled in the
>context of a request so you can dispatch based on header info etc.

As long as some part of the expression is a zero-argument callable, 
dependent subexpressions will be evaluated at dispatch time.  e.g. 
"get_request().foo" will be entirely evaluated at runtime.




More information about the PEAK mailing list