En Thu, 24 Sep 2009 12:14:39 -0300, brian huggins <bgh...@aol.com> escribió:

I want to set up descriptors at runtine, but it isn't working the way
i would expect.  Does anybody know if this is possible?

class TestDesc (object):
    x=Descriptor ("x")
    def __init__ (self):
        self.y=Descriptor("y")

test=TestDesc()
test.y
<descriptor.Descriptor object at 0x00B3A5F0>

When i access y, it appears to be a regular object and not a
descriptor type object like x.  Is there a way to make y work the same
as x but setting it up during object creation?

The descriptor "magic" only happens when the attribute is looked up on the class; when it's found in the instance itself, nothing special happens. You cannot attach descriptors to a specific instance, but you can alter its class at runtime. Basically you have to create a new class (that inherits from the "real" class and adds the desired descriptor) and set it as the instance __class__ attribute. (This was discussed some time ago, try searching the list archives. Uh, I found this: http://groups.google.com/group/comp.lang.python/t/bfc093464dd6ba9/
but you should be able to find other threads)

--
Gabriel Genellina

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

Reply via email to