PEP 318: Function Modifier Syntax

Gerrit Holl gerrit at nl.linux.org
Mon Jun 9 05:34:03 EDT 2003


Hi,

A new PEP was checked in:
>     Various syntaxes have been proposed in comp.lang.python.  The
>     most common are demonstrated below.
> 
>         def foo(self) [synchronized(lock), classmethod]:
>             perform method operation
> 
>         def foo(self) {'pre': synchronized(lock), 'classmethod': True}:
>             """ Skip Montanaro syntax """
>             perform method operation
> 
>         def foo(self) as synchronized(lock), classmethod:
>             """ Gerrit Holl syntax """
>             perform method operation

This wasn't mine actually. Mine was:

def classmethod(foo(self))
def synchonized(classmethod(foo(self)), lock)

However, def foo as bar is IMHO more readable, so I will happily
accept def foo as bar and withdraw my syntax as a 2nd choice.

>         def foo(self) as synchronized(lock), classmethod:
>             perform method operation
> 
>     More generally, 
> 
>         def foo(self) as <tuple>:
>             perform method operation

A <tuple> would imply a obliged trailing comma if only one modifier is used.
I would say:

funcdef: "def" funcname "(" [parameter_list] ")" [call_list] ":"
call-list: callable ("," callable)* [","]

The trailing comma is also allowed in parameter_list, although I've never
seen it:

  0 >>> def f(a,): return a
  0 ...
  1 >>> f(3)
3

But now the properties: those are more difficult.
A legal syntax would be:

def __get(self) as property: ...

However, this doesn't work for set and del. Using function attributes to
accomplish properties, as has been proposed in the past, is a different
story and would require a different PEP.

Maybe:
    class Foo(object):
        bar = property()
        def __get(self) as bar.get:
            ...
        def __set(self, parrot) as bar.set:
            ...
        def __del(self) as bar.del_:
            ...

This would mean adding three methods to a property object,
get, set and del_ (need a good name for that!), that set bar.fget,
bar.fset and bar.fdel. Currently, those are readonly attributes,
so implementing it in Python seems impossible.

yours,
Gerrit.

-- 
245. If any one hire oxen, and kill them by bad treatment or blows, he
shall compensate the owner, oxen for oxen.
        -- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list