[Grok-dev] Re: you can comma separate directives

Philipp von Weitershausen philipp at weitershausen.de
Fri May 2 04:07:59 EDT 2008


Brandon Craig Rhodes wrote:
> What fun!  It just occurred to be that, because directives just
> introspect the stack, but don't return any useful value "into" the
> class-declaration code block they're part of, you can stack them on a
> single line with commas:
> 
>     class Fish(grok.View):
>         grok.context(MyApp), grok.name('fish')
> 
> That will be helpful the next time a stack of directives threatens to
> push my actual code down off of the bottom of the screen.

You could've also used a semicolon :). But yeah, it's pretty cool, 
albeit a bit confusing...

> I suppose that would mean that they would work *wherever* in a line of
> class-declaration code they were evaluated, even if they were part of
> an expression, or an argument to a function.  So the above could be
> written:
> 
>     class Fish(grok.View):
>         grok.context(grok.name('fish') or MyApp)
> 
> Which is useless, of course, but silly examples like this help me get
> straight about what really makes directives work: simply by being
> invoked *somewhere* on a line of code inside of a class declaration,
> they get automatic access the class object.

Actually, the class object hasn't been created at that stage yet. All 
they get access to is the local stack frame that's been created to 
collect all the class-level variables (including functions that become 
methods on the class). This stack frame dict is then passed to the class 
constructor (type or whatever your metaclass is).

> Somewhere in the realm of possibilities this opens up is an
> interesting syntax construction that I'm not thinking of. :-)

Not sure what the good news is... that it opens up those syntax 
possibilities or that you're not thinking of them ;).


More information about the Grok-dev mailing list