[Python-Dev] Defining properties - a use case for class decorators?
Nick Coghlan
ncoghlan at gmail.com
Tue Oct 18 15:59:03 CEST 2005
Jim Jewett wrote:
> That said, I'm not sure the benefit is enough to justify the
> extra complications, and your suggestion of allowing strings
> for method names may be close enough. I agree that the
> use of strings is awkward, but ... probably no worse than
> using them with __dict__ today.
An idea that was kicked around on c.l.p a long while back was "statement local
variables", where you could define some extra names just for a single simple
statement:
x = property(get, set, delete, doc) given:
doc = "Property x (must be less than 5)"
def get(self):
try:
return self._x
except AttributeError:
self._x = 0
return 0
def set(self, value):
if value >= 5: raise ValueError("value too big")
self._x = x
def delete(self):
del self._x
As I recall, the idea died due to problems with figuring out how to allow the
simple statement to both see the names from the nested block and modify the
surrounding namespace, but prevent the names from the nested block from
affecting the surrounding namespace after the statement was completed.
Another option would be to allow attribute reference targets when binding
function names:
x = property("Property x (must be less than 5)")
def x.get(instance):
try:
return instance._x
except AttributeError:
instance._x = 0
return 0
def x.set(instance, value):
if value >= 5: raise ValueError("value too big")
instance._x = x
def x.delete(instance):
del instance._x
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.blogspot.com
More information about the Python-Dev
mailing list