Any syntactic cleanup likely for Py3? And what about doc standards?

Ferenczi Viktor python at cx.hu
Wed Sep 5 19:19:28 EDT 2007


Hello,

> > from property_support import hasProperties, Property
> >
> > @hasProperties
> > class Sphere(object):
> >     def setRadius(self, value):
> >         ... some setter implementation ...
> >     radius=Property(default=1.0, set=setRadius, type=(int, float))
> >     color=Property(default='black', allowNone=True)
> >
> > This is a cleaner syntax if you need automatic default
> > setter/getter implementations with type checking, default values,
> > etc.
>
> Well, I think @hasProperties is very ugly.  Such things always look
> like a dirty trick.  The programmer wonders why the language cannot
> detect by itself that there are properties in this class.

If you do not include the @hasProperties class decorator, then 
radius=Property(...) means that radius is a class attribute with a Property 
instance as it's value, nothing special. If you also include the class 
decorator, then it takes an additional meaning. In this case the decorator 
should remove the Property instance from the class and turn it into a real 
property. This is not magic. This is a possible use case of the new class 
decorator feature.

Properties are very useful, since ordinary attribute access can be 
transparently replaced with properties if the developer needs to add code 
when it's set or needs to calculate it's value whenever it is read.

As an additional benefit this could allow developers to implement change 
events for properties that allows any number of observers to monitor property 
changes. This could be very useful in GUI programming, for example.

Regards, Viktor



More information about the Python-list mailing list