[Python-Dev] Call for defense of @decorators

Bob Ippolito bob at redivi.com
Thu Aug 5 20:59:04 CEST 2004


On Aug 5, 2004, at 2:48 PM, Gustavo Niemeyer wrote:

> Hello Ronald,
>
>> I'm in favor of @decorators. It's easy to notice them when the are
>> present and they are clearly special. The '[decorator] def ...' and
>
> 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.

>> 'decorate(decorator) def ...' are very magic, and are IMHO unfriendly
>> to newbies (you must metion them in any introduction to python, 
>> because
>> otherwise users would complety mis the signicance of the decorations).
> [...]
>> 	@objc.signature("v@:@i")
>> 	def saveSheetDidDismiss_returnCode_contextInfo_(self, sheet,
>> returnCode, contextInfo):
>> 		pass
>>
>> The argument to objc.signature is fairly magic and I do try to 
>> abstract
>> it away (such as with the introduction of objc.accessor), but that's
>> not always possible. If decorators were not allowed to have arguments
>> I'd have to introduce temporary functions that wouldn't help in
>> readability.
>
> Is special syntax in the language really required in this case,
> considering you're already doing something "fairly magic" anyways?

The alternative would be (current syntax and current PyObjC) this:

def saveSheetDidDismiss_returnCode_contextInfo_(self, sheet, 
returnCode, contextInfo):
	pass
saveSheetDidDismiss_returnCode_contextInfo_ = 
objc.selector(saveSheetDidDismiss_returnCode_contextInfo_, 
signature='v@:@i')

Yes, we pretty much do need special syntax (without using a hack like 
PJE's).  Code like this is pretty commonplace in PyObjC projects.

>> 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.

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.

-bob


More information about the Python-Dev mailing list