PEP 318: Function Modifier Syntax
Duncan Booth
duncan at NOSPAMrcp.co.uk
Mon Jun 9 06:43:10 EDT 2003
Gerrit Holl <gerrit at nl.linux.org> wrote in
news:mailman.1055151317.13691.python-list at python.org:
> I would say:
>
> funcdef: "def" funcname "(" [parameter_list] ")" [call_list] ":"
> call-list: callable ("," callable)* [","]
Did you miss out "as" before the [call_list]? Also, I still think that you
need to allow optional parentheses around the call-list.
> 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.
>
What would people think of a half-way house where the new syntax is used
only on the last part of the definition, and you define the other methods
just as you do just now?
Since the property type isn't callable today, I think this just about fits
without breaking anything:
Define a readonly property, implicitly calls property(X) which creates a
readonly property:
def X(self) as property:
return self.__x_value
Define a readwrite property, the call to property returns a property object
which is itself callable. Calling a property object with an fset but no
fget would take the parameter as a new fget:
def set_X(self, value):
...
def X(self) as property(fset=set_X):
return self.__x_value
or
def get_X(self):
...
def X(self) as property(fget=get_X):
...
Defining a writeonly property:
def X(self, value) as property(fget=None):
...
Another thought. When defining a function I can do:
f = 3
def f(x=f): pass
The parameter name x doesn't conflict with access to the variable x.
The same thing should happen in:
funcdef: "def" funcname "(" [parameter_list] ")" "as" [call_list] ":"
any reference to a variable in callable with the same name as funcname
should get the previous value of funcname (if there was one).
So it would be possible to write:
def X(self) as property:
return self.__x_value
def X(self, value) as X:
print "setting X"
which sucks, although if property could take a property as a parameter you
might be able to write this instead:
def X(self) as property:
return self.__x_value
def X(self, value) as property(fget=X):
print "setting X"
Or you could simply define some new wrappers property_get, property_set,
property_del and write:
def X(self) as property_get: ...
def X(self, value) as property_set(X): ...
def X(self) as property_del(X): ...
with appropriate but confusing definitions.
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list