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

Thomas Heller theller at python.net
Thu Feb 19 13:36:35 EST 2004


Bob Ippolito <bob at redivi.com> writes:

> 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

I was thinking of a similar usecase for ctypes, defining C callback
functions.  In ctypes, you would be able to write

"""
WndProc = WINFUNCTYPE(c_int, HWND, MSG, WPARAM, LPARAM)

def my_wndproc(hwnd, msg, wParam, lParam) [WndProc]:
    ....
"""

instead of this

"""
WndProc = WINFUNCTYPE(c_int, HWND, MSG, WPARAM, LPARAM)

def my_wndproc(hwnd, msg, wParam, lParam):
    ....

my_wndproc = WndProc(my_wndproc)
"""

BTW (I have this patch not yet applied), is it possible to use more than
one line, in this way?

def function(arg1, arg2, arg3, arg4, arg5, arg6)
        [dothis, dothat, doanother]:

All in all, I'm +1 on the patch.

Thomas




More information about the Pythonmac-SIG mailing list