Welcome, guest | Sign In | My Account | Store | Cart
class NamedDescriptor(object):
    def __init__(self):
        # Name of the attribute, will be set by the instances of the meta class DescriptorNameGetterMetaClass
        self.name = None

    def __get__(self, obj, type=None):
        if obj:
            # at the moment, we return the name of the descriptor, you can change this line to adapt to your needs
            return self.name
            return self

    def __set__(self, adaptor, value):
        if adaptor:
            # change the name, you can change this line to adapt to your needs
            self.name = value
            raise AttributeError('Cannot set this value')

class NamedDescriptorResolverMetaClass(type):
    def __new__(cls, classname, bases, classDict):
        for name, attr in classDict.iteritems():
            if isinstance(attr, NamedDescriptor):
                attr.name = name
        return type.__new__(cls, classname, bases, classDict)

class DescriptorHost(object):
    The Descriptor gets its name automatically even before any instance of the class is created, thanks MetaClass!
    >>> DescriptorHost.myFunDescriptor.name

    In our case, the NamedDescriptor returns it's name when being looked for from the host instance:
    >>> dh = DescriptorHost()
    >>> dh.myFunDescriptor
    __metaclass__ = NamedDescriptorResolverMetaClass

    myFunDescriptor = NamedDescriptor()

if __name__ == "__main__":
    import doctest