descriptor problems

Peter Otten __peter__ at web.de
Thu Sep 14 05:53:40 EDT 2006


Gary Stephenson wrote:

> I want to define a class-level attribute that will be shared by all
> subclasses.  That is, I want this class, every subclass of this class,
> every instance of this class and every instance of every subclass to be
> able to
> read and write to the _same_ slot/variable.  But I can't figure out how to
> do it.

class A:
    class __metaclass__(type):
        _shared = 42
        def get_shared(self):
            return self.__class__._shared
        def set_shared(self, value):
            print "%r --> %r" % (self._shared, value)
            self.__class__._shared = value
        shared = property(get_shared, set_shared)

    def get_shared(self):
        return self.__class__.shared
    def set_shared(self, value):
        self.__class__.shared = value
    shared = property(get_shared, set_shared)

Does that what you want? 

Peter




More information about the Python-list mailing list