[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

function decorator-like function

vsoler

3/27/2010 3:24:00 PM

Hi,

Still learning Python, now decorators.

Before diving deeply into decorators, I'd like to apply a function to
another function.

My "extremely simple function" should print number 3, then the sum of
its 2 arguments.

Say that I call f(5,7)
I'd like to get, somehow, 3 then 12.

I've tried the following:

def d(f):
print 3
return f

def f(a, b):
print a+b

f=d(f)

However, it does not work. Calling f(5,7) only returns 12, (3 is
missing)
How should I proceed?
6 Answers

Patrick Maupin

3/27/2010 4:07:00 PM

0

On Mar 27, 10:24 am, vsoler <vicente.so...@gmail.com> wrote:
> Hi,
>
> Still learning Python, now decorators.
>
> Before diving deeply into decorators, I'd like to apply a function to
> another function.
>
> My "extremely simple function" should print number 3, then the sum of
> its 2 arguments.
>
> Say that I call   f(5,7)
> I'd like to get, somehow, 3 then 12.
>
> I've tried the following:
>
> def d(f):
>     print 3
>     return f
>
> def f(a, b):
>     print a+b
>
> f=d(f)
>
> However, it does not work. Calling f(5,7) only returns 12, (3 is
> missing)
> How should I proceed?

>>> def d(f):
.... def wrapper(*args):
.... print 3
.... return f(*args)
.... return wrapper
....
>>> def f(a, b):
.... print a + b
....
>>> f = d(f)
>>> f(5, 7)
3
12

HTH,
Pat

vsoler

3/27/2010 4:21:00 PM

0

On 27 mar, 17:06, Patrick Maupin <pmau...@gmail.com> wrote:
> On Mar 27, 10:24 am, vsoler <vicente.so...@gmail.com> wrote:
>
>
>
> > Hi,
>
> > Still learning Python, now decorators.
>
> > Before diving deeply into decorators, I'd like to apply a function to
> > another function.
>
> > My "extremely simple function" should print number 3, then the sum of
> > its 2 arguments.
>
> > Say that I call   f(5,7)
> > I'd like to get, somehow, 3 then 12.
>
> > I've tried the following:
>
> > def d(f):
> >     print 3
> >     return f
>
> > def f(a, b):
> >     print a+b
>
> > f=d(f)
>
> > However, it does not work. Calling f(5,7) only returns 12, (3 is
> > missing)
> > How should I proceed?
> >>> def d(f):
>
> ...     def wrapper(*args):
> ...         print 3
> ...         return f(*args)
> ...     return wrapper
> ...>>> def f(a, b):
>
> ...     print a + b
> ...>>> f = d(f)
> >>> f(5, 7)
>
> 3
> 12
>
> HTH,
> Pat

Pat,

I think some lines are missing. I don't see "d" function defined. Any
lines above def wrapper?

Thank you

vsoler

3/27/2010 4:25:00 PM

0

On 27 mar, 17:21, vsoler <vicente.so...@gmail.com> wrote:
> On 27 mar, 17:06, Patrick Maupin <pmau...@gmail.com> wrote:
>
>
>
> > On Mar 27, 10:24 am, vsoler <vicente.so...@gmail.com> wrote:
>
> > > Hi,
>
> > > Still learning Python, now decorators.
>
> > > Before diving deeply into decorators, I'd like to apply a function to
> > > another function.
>
> > > My "extremely simple function" should print number 3, then the sum of
> > > its 2 arguments.
>
> > > Say that I call   f(5,7)
> > > I'd like to get, somehow, 3 then 12.
>
> > > I've tried the following:
>
> > > def d(f):
> > >     print 3
> > >     return f
>
> > > def f(a, b):
> > >     print a+b
>
> > > f=d(f)
>
> > > However, it does not work. Calling f(5,7) only returns 12, (3 is
> > > missing)
> > > How should I proceed?
> > >>> def d(f):
>
> > ...     def wrapper(*args):
> > ...         print 3
> > ...         return f(*args)
> > ...     return wrapper
> > ...>>> def f(a, b):
>
> > ...     print a + b
> > ...>>> f = d(f)
> > >>> f(5, 7)
>
> > 3
> > 12
>
> > HTH,
> > Pat
>
> Pat,
>
> I think some lines are missing. I don't see "d" function defined. Any
> lines above def wrapper?
>
> Thank you

Patrick,

I see what happened. The first line was somehow hidden.
Thank you very much.

Vicente Soler

Patrick Maupin

3/27/2010 6:08:00 PM

0

On Mar 27, 11:24 am, vsoler <vicente.so...@gmail.com> wrote:
> I see what happened. The first line was somehow hidden.
> Thank you very much.

You're welcome. Sorry about the formatting. Also, note that if your
decorator is complicated, you might want to use a class instead of a
nested function. Here's the same thing, using a class (and using the
actual decorator syntax):

class d(object):
def __init__(self, func):
self.func = func
def __call__(self, *args):
print 3
return self.func(*args)

@d
def f(a, b):
print a + b

f(5, 7)

Pat

Michele Simionato

3/28/2010 2:17:00 PM

0

Another option is to use my own decorator module (http://
pypi.python.org/pypi/decorator):

from decorator import decorator

@decorator
def d(func, *args):
print 3
return func(*args)

@d
def f(a, b):
print a + b

f(5, 7)


Patrick Maupin

3/28/2010 4:27:00 PM

0

On Mar 28, 9:17 am, Michele Simionato <michele.simion...@gmail.com>
wrote:
> Another option is to use my own decorator module (http://
> pypi.python.org/pypi/decorator):
>
> from decorator import decorator
>
> @decorator
> def d(func, *args):
>     print 3
>     return func(*args)
>
> @d
> def f(a, b):
>     print a + b
>
> f(5, 7)

That looks cool (and I'm glad you mentioned it), but it appeared to me
the OP was trying to manually construct the equivalent of a decorator
without the "@" syntax, in order to gain an understanding of how
decorators work, and for this particular purpose, piling additional
magic on top of decorators is probably not helpful :-)

Regards,
Pat