Antoon Pardon wrote:
Can anyone explain why descriptors only work when they are an attribute
to an object or class.  I think a lot of interesting things one can
do with descriptors would be just as interesting if the object stood
on itself instead of being an attribute to an other object.

Not sure what "stood on itself" really means, but if you just want to be able to have module-level properties, you can do something like:


py> class Module(object):
...     oldimporter = __builtins__.__import__
...     def __init__(self, *args):
...         mod = self.oldimporter(*args)
...         self.__dict__ = mod.__dict__
...     p = property(lambda self: 42)
...
py> __builtins__.__import__ = Module
py> import __main__
py> __main__.p
42
py> __main__.p = 3
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: can't set attribute

where you replace module objects with a different object. Note that this is also nasty since properties reside in the class, so all modules now share the same properties:

py> import sys
py> sys.p
42
py> sys.p = 13
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: can't set attribute

STeVe
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to