[Pythonmac-SIG] Plea for function/method syntax sugar (PEP 318, with a different syntax)

Bob Ippolito bob at redivi.com
Wed Feb 18 21:26:33 EST 2004


Some time ago, mwh developed a patch that adds some syntactical sugar  
to def, which is equivalent to PEP 318 though it has a different and  
more flexible syntax...
previous threads can be easily found here:   
http://www.google.com/search?q=+site:mail.python.org+%22meth-syntax- 
sugar%22
the latest version of mwh's patch is here:  
http://starship.python.net/crew/mwh/hacks/meth-syntax-sugar-3.diff

Here's a quick overview:

def foo(args) [sugary, expressions, list]:
	pass

This is equivalent to:

def foo(args):
	pass
foo = list(expressions(sugary(foo)))

This evaluation order is Guido approved, though at least one person  
wanted it to be the other way around

One would use this in scenarios such as:

class FooClass(object):
     def className(klass) [classmethod]:
         return klass.__name__

or, even more importantly (to me anyway):

# we would change PyObjC to make this a built-in feature.. but, for  
completeness:
import objc
def signature(sig):
     def _signature(fn):
         return objc.selector(fn, signature=sig)
     return _signature

class FooClass(NSObject):
     def returnsAnInteger(self) [signature('i@:')]:
         return 1
     def returnsVoidTakesAnInteger_(self, anInteger) [signature('v@:i')]:
         pass

With current syntax, PyObjC is extremely cumbersome:

class FooClass(NSObject):
     def returnsAnInteger(self):
         return 1
     returnsAnInteger = objc.selector(returnsAnInteger, signature='i@:')

     def returnsVoidTakesAnInteger_(self, anInteger):
         pass
     returnsVoidTakesAnInteger_ =  
objc.selector(returnsVoidTakesAnInteger_, signature='v@:i')
     # these are actually short examples, compare to something like:
     # textView_completions_forPartialWordRange_indexOfSelectedItem_

Why we need this:
	Without it, it's hard use PyObjC correctly.  ObjC selector  
nomenclature is extremely verbose, and your fingers hurt after a while  
having to type each function name three times.  The function __name__  
is important, because the objc.selector type has to convert it to a  
selector:that:uses:colons:, or else the colon:using:name: would have to  
be specified manually.
	It makes classmethod/staticmethod/etc more palatable.

What the patch doesn't do:
	lambda is not allowed in the "sugary expressions list"
	there's no *expansion and it won't take an actual list so if you want  
a prebaked list of transformations then you'll have to roll a callable  
that does it such as:

def prebake(*expressions):
     def _prebake(fn):
         for transformation in expressions:
             fn = transformation(fn)
         return fn
     return fn

This syntactical sugar for def is so unbelievably important to PyObjC  
(and likely other projects) that I am willing to distribute my own  
modified version of Python if it doesn't make 2.4 (though I would  
probably use Stackless as the base, but that's another plea for another  
day).

The patch looks like it still applies to CVS HEAD, but there is a  
little fuzz on hunk 2.  I have not tested it yet, but I have tested it  
against CVS HEAD of the 2.3.3 based Stackless.

I'm willing to help however I can in order to get this into Python 2.4.

-bob




More information about the Pythonmac-SIG mailing list