[Python-Dev] Re: Extended Function syntax
Manuel Garcia
news@manuelmgarcia.com
Wed, 29 Jan 2003 19:21:26 -0800
Maybe something like this would be nice:
xdef myprop('myprop docstring') [property]:
def __get__(self):
...body...
def __set__(self, value):
...body...
xdef frame1() [SunkenFrame]:
xdef button1('OK') [Button]:
def ClickEvent():
...body...
def GotFocus():
...body...
MouseoverText = 'Hello!'
xdef text1(datasource=xyz) [TextBox]: pass
xdef klass(base1, base2) [interface1, interface2]:
...class body...
This is almost exactly what Arne Koewing posted:
blockdef myprop [property]:
def __get__(self):
...
def __set__(self, value):
...
def __delete__(self):
...
Except I tried to make it look a bit more like 'def'.
Obviously, this would go hand-in-hand with 'def' being extended to
allow a list of modifiers at the end, like this:
def name(args) [staticmethod]:
The general syntax for 'xdef' would be something like:
xdef name(arg1, arg2, kw_arg1=x, kw_arg2=y) [mod1, mod2]:
...class-like body...
So this would work like:
name = mod2(mod1(name, args, kw_args, dict))
I guess args and kw_args would work something like how 'bases' are
collected in class definitions before being passed to metaclass
__new__ and __init__.
'xdef' could return a class just as well as a property:
xdef klass(base1, base2) [interface1, interface2]:
...class body...
'interface1','interface2' would have to be clever about sometimes
being passed a class and sometimes being passed the tuple (name, args,
kw_args, dict).
In general, 'xdef' would allow metaclass-like processing on things
that are not necessarily a class.
Problems:
(1) The main problem is that 'xdef' makes things that are pretty close
to being classes. So why not just use classes?
(2) Also 'xdef' is pointless without [mod1, mod2]
xdef name():
would just assign the tuple (name, args, kw_args, dict) to name.
(3) 'xdef' isn't a great name, because
xdef abc(xyz):
def abc(xyz):
do completely different things. But the name should be short like
'def' so it doesn't draw much attention to itself, just as 'def'
doesn't draw much attention to itself. So
xdef myprop() [property]:
the thing that sticks out is 'property'
More bad stuff:
xdef abc(xyz):
return 7
would raise a 'SyntaxError: 'return' outside function'
xdef name(arg1, kw_arg1=x) [mod1, mod2]:
y = arg1 + kw_arg1
would raise NameError.
Maybe Arne Koewing has a good idea not making it resemble 'def'.