[Python-Dev] properties and block statement

Stefan Rank stefan.rank at ofai.at
Wed Oct 19 09:01:21 CEST 2005


on 18.10.2005 19:17 Antoine Pitrou said the following:
>>    What would this mythical block statement look like that would make
>>properties easier to write than the above late-binding or the subclass
>>Property recipe?
> 
> I suppose something like:
> 
> class C(object):
>     x = prop:
>         """ Yay for property x! """
>         def __get__(self):
>             return self._x
>         def __set__(self, value):
>             self._x = x
> 
> and then:
> 
> def prop(@block):
>     return property(
>         fget=block.get("__get__"),
>         fset=block.get("__set__"),
>         fdel=block.get("__delete__"),
>         doc=block.get("__doc__", ""),
>     )
> 
> (where "@bargs" would be the syntax to refer to block args as a dict,
> the same way "**kargs" already exist)
> 

I think there is no need for a special @syntax for this to work.

I suppose it would be possible to allow a trailing block after any 
function invocation, with the effect of creating a new namespace that 
gets treated as containing keyword arguments.

No additional function needed for the property example::

   class C(object):
       x = property():
           doc = """ Yay for property x! """
           def fget(self):
               return self._x
           def fset(self, value):
               self._x = x


(This does not help with the problem of overridability though...)

A drawback is that such a "keyword block" would only be possible for the 
last function invocation of a statement.
Although the block could also be inside the method invocation 
parentheses? I do not think that this is a pretty sight but I'll spell 
it out anyways ;-) ::

   class C(object):
       x = property(:
           doc = """ Yay for property x! """
           def fget(self):
               return self._x
           def fset(self, value):
               self._x = x
       )


--stefan



More information about the Python-Dev mailing list