[Python-Dev] property syntax (original) (raw)

john coppola john_coppola_r_s@yahoo.com
Fri, 15 Feb 2002 16:34:47 -0800 (PST)


Hey Fred. It might not be a good idea to nest the "property class" like an inner class. It may be plausible that property objects are reusable between classes. As implied by this syntax, it wouldn't be reuseable. Another point, is that they may be very large. Which would be messy.

I did i bit of brainstorming.

One purpose of the type objects is a means to coerce one object to another. So here is the pattern.

Just like str(MyObject) requires str, or len(MyObject) requires len or any of the factory functions for that matter, the property factory function would require that your object support both get , set , and del. Thats it. So instead of, property(fset,fget,fdel)you would instead have, property(AnyObjectSupportingAboveInterface).

How the property factory function differs from the others is that it will only check for the existence of these methods, and will not execute the code within them. It instead sets a flag on the object indicating that it is active. Will be necessary to do checking on every object for every set, or every get. Not too bad though. How time consuming is two if statements?

Is this making sense?

John Coppola

--- "Fred L. Drake, Jr." <fdrake@acm.org> wrote:

Fred L. Drake, Jr. writes: [describing a suggested property syntax] > class Foo(object): > property myprop: > """A computed property on Foo objects.""" > > def get(self): > return ... Perhaps it was obvious to everyone else, but it just occured to me that this lends itself to inheriting descriptor types:

class ReadOnly(object): def get(self): raise NotImplementedError("sub-class must override this!") def set(self): raise AttributeError("read-only attribute") def delete(self): raise AttributeError("read-only attribute") class Foo(object): property myprop(ReadOnly): def get(self): return ...

-Fred -- Fred L. Drake, Jr. PythonLabs at Zope Corporation


Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev


Do You Yahoo!? Yahoo! Sports - Coverage of the 2002 Olympic Games http://sports.yahoo.com