method annotations

8 views
Skip to first unread message

Terrence Brannon

unread,
Sep 25, 2008, 11:04:48 AM9/25/08
to Nagare users
Hello, I've been a fairly self-taught Python programmer. I'm really
interested in Nagare, having seen the sample apps.

I have never been exposed to method annotations such as this:
http://www.nagare.org/trac/browser/trunk/nagare/examples/nagare/examples/counter.py
@presentation.render_for(Counter1)
def render(self, h, comp, *args):


and am wondering how they work. Googling docs.python.org for
annotations:
http://www.google.com/search?q=site%3Adocs.python.org%20annotations

yields no help.

apoirier

unread,
Sep 25, 2008, 1:51:30 PM9/25/08
to Nagare users
Hi Brannon,

This annotation is called a "decorator". The short answer is that

@presentation.render_for(Counter1)
def render(self, h, comp, *args):
...

is syntaxic sugar for:

def render(self, h, comp, *args):
...
render = presentation.render_for(Counter1)(render)

The formal answer is at: http://www.python.org/dev/peps/pep-0318/

On 25 sep, 17:04, Terrence Brannon <metap...@gmail.com> wrote:
> Hello, I've been a fairly self-taught Python programmer. I'm really
> interested in Nagare, having seen the sample apps.
>
> I have never been exposed to method annotations such as this:http://www.nagare.org/trac/browser/trunk/nagare/examples/nagare/examp...

Terrence Brannon

unread,
Sep 29, 2008, 2:52:09 PM9/29/08
to Nagare users


On Sep 25, 1:51 pm, apoirier <alain.poiri...@gmail.com> wrote:
> Hi Brannon,
>
> This annotation is called a "decorator". The short answer is that
>
>     @presentation.render_for(Counter1)
>     def render(self, h, comp, *args):
>         ...
>
> is syntaxic sugar for:
>
>     def render(self, h, comp, *args):
>         ...
>     render = presentation.render_for(Counter1)(render)

I've looked at a few tutorials on Decorators, but
1 - none of them use method call notation in them, such as your
presentation.render_for
2 - The sequence of parenthetical expressions confuses me ....
"""(Counter1)(render)"""
is that parsed as:
tmp = presentation.render_for(Counter1)
render = presentation.tmp(render)

But I have the basic idea. The function render is being 'wrapped' such
that some other things take the core function as an argument then
produce a version of render that cooperates with the calling
conventions of the framework.

It's being used to keep the Model free of View code



apoirier

unread,
Sep 29, 2008, 4:35:48 PM9/29/08
to Nagare users
On Sep 29, 8:52 pm, Terrence Brannon <metap...@gmail.com> wrote:
> On Sep 25, 1:51 pm, apoirier <alain.poiri...@gmail.com> wrote:
>
> > Hi Brannon,
>
> > This annotation is called a "decorator". The short answer is that
>
> >     @presentation.render_for(Counter1)
> >     def render(self, h, comp, *args):
> >         ...
>
> > is syntaxic sugar for:
>
> >     def render(self, h, comp, *args):
> >         ...
> >     render = presentation.render_for(Counter1)(render)
>
> I've looked at a few tutorials on Decorators,  but
> 1 - none of them use method call notation in them, such as your
> presentation.render_for
For can find such an example in the section "nested functions" of
http://muharem.wordpress.com/2006/10/19/python-decorator-mini-study-part-2-of-3/

> 2 - The sequence of parenthetical expressions confuses me ....
> """(Counter1)(render)"""
> is that parsed as:
> tmp    = presentation.render_for(Counter1)
> render = presentation.tmp(render)
Yes, exactly.

> But I have the basic idea. The function render is being 'wrapped' such
> that some other things take the core function as an argument  then
> produce a version of render that cooperates with the calling
> conventions of the framework.
You're totally right on the mecanism ...

> It's being used to keep the Model free of View code
... and on the purpose
Reply all
Reply to author
Forward
0 new messages