[Python-Dev] Call for defense of @decorators

Bob Ippolito bob at redivi.com
Thu Aug 5 21:28:58 CEST 2004


On Aug 5, 2004, at 3:17 PM, Gustavo Niemeyer wrote:

> Hi Bob,
>
>>> Why are they special? Why should they be more important than any 
>>> other
>>> part of the function definition?
>>
>> Because they take a function object as input and can do whatever they
>> want with it and return something else.
>
> This seems extremely powerful. OTOH, perl is also powerful.

List comprehensions, generators, and generator expressions are also 
very powerful.  Let's get rid of those too.  What good is *args, 
**kwargs syntax if we can do it with apply?  Why do we need a syntax to 
build dictionaries if we can just dict() and then set all of the keys 
one at a time like in most other languages?

>>>> def saveSheetDidDismiss_returnCode_contextInfo_(self, sheet,
>>
>>> What is objc.signature() doing?
>>
>> objc.signature wraps the function object with an objc.selector that
>> specifies specific return and argument types.  In this particular 
>> case,
>> it declares that the selector
>> saveSheetDidDismiss:returnCode:contextInfo: returns void and takes an
>> object and an integer as arguments.  Without this, the selector can 
>> not
>> be bridged correctly to the Objective C runtime and the program would
>> crash.
>
> Isn't metaclass usage helpful in this case?
>
> Or a perhaps a dictionary?
>
>    __signature__ = {"funcname": "v@:@i"}
>
> or
>
>    def funcname(...):
>       ...
>    funcname.signature = "v@:@i"
>
> and a postprocessor like:
>
> objc.register(classname)

The signature is tightly bound to the arguments that the function 
takes.  The farther you move it away from the arguments the easier it 
is to make a difficult to diagnose mistake (in the case of PyObjC, a 
segfault hopefully either upon call or return, but quite possibly some 
time later).  Metaclasses also do not always play very well with each 
other.  Decorators are much simpler than metaclasses in concept and 
implementation.

>> The ctypes package behaves similarly and would use decorators for the
>> same thing.  I imagine that other runtime/language bridges would also
>> benefit from similar techniques (Jython, IronPython, Python.NET,
>> JPython.. or whatever else).  I can also imagine it being used for
>> things like XML-RPC, SOAP, Apple Events, COM, etc. in a similar 
>> manner.
>
> Is this something good? I mean, having function wrappers all
> around the place? Wouldn't it be introducing unobvious code (magic?)
> in packages which are working fine as they are?

I think you're mistaken if you think that all of these sorts of things 
work just great as-is.  It should be pretty obvious what a decorator 
with a sensible name does, just like when you subclass something or 
raise a particular kind of exception it should be pretty obvious what 
behavior that is going to have.  I know what "raise SystemExit" does, 
just like I know what @objc.signature(...) or @classmethod would do.

-bob


More information about the Python-Dev mailing list