Docorator Disected

Ron_Adam radam2 at tampabay.rr.com
Sun Apr 3 00:09:07 EST 2005


On 2 Apr 2005 20:02:47 -0800, "El Pitonero" <pitonero at gmail.com>
wrote:

>Ron_Adam wrote:
>>
>> So I didn't know I could do this:
>>
>> def foo(a1):
>>     def fee(a2):
>>         return a1+a2
>>     return fee
>>
>> fum = foo(2)(6)   <------ !!!
>
>Ah, so you did not know functions are objects just like numbers,
>strings or dictionaries. I think you may have been influenced by other
>languages where there is a concept of static declaration of functions.

No, I did not know that you could pass multiple sets of arguments to
nested defined functions in that manner.  Just haven't ran acrossed it
in the two years I've been playing around with python. I haven't had a
reason to try it either.  But maybe now that I'm aware of it, I'll
find more uses for it.



>The last line can be better visualized as:
>
>fum = (foo(2)) (6)
>
>where foo(2) is a callable.
>
>-----------
>
>Since a function is an object, they can be assigned (rebound) to other
>names, pass as parameters to other functions, returned as a value
>inside another function, etc. E.g.:
>
>def g(x):
>    return x+3
>
>h = g # <-- have you done this before? assignment of function

Sure, I have no problem with that. Been doing it for quite a while. :)

>print h(1) # prints 4
>
>def f(p):
>    return p # <-- function as return value
>
>p = f(h) # <-- passing a function object
>
>print p(5) # prints 8
>
>Python's use of "def" keyword instead of the "=" assignment operator
>makes it less clear that functions are indeed objects. As I said
>before, this is something to think about for Python 3K (the future
>version of Python.)

I've always equated 'def' as if it were 'make', or in Python its just
a variation of 'class' for a subset of objects of type 'function'.  

>------------
>
>Function modifiers exist in other languages. Java particularly is
>loaded with them.
>
>public static synchronized double random() {
>...
>}
>
>So your new syntax:
>
>@decorator(a1)(foo)
>def foo(): 
>   pass 
>
>is a bit out of the line with other languages.

So?  Why would it need to be the same as other languages?  I like
Python because it's not the same. :)

The above syntax suggestion, just matches the already existing
behavior,

Thanks for helping BTW,  I think I have it down pretty good now. 

Cheers,
Ron





More information about the Python-list mailing list