__set__ method is not called for class attribute access

Ryan heniser at yahoo.com
Fri Aug 5 13:29:06 CEST 2011

In the context of descriptors, the __set__ method is not called for
class attribute access. __set__ is only
called to set the attribute on an instance instance of the owner class
to a new value, value. WHY? Is there some other mechanism for
accomplishing this outcome. This subtle difference from __get__cost me
some time to track down. Might think about pointing that out the

class RevealAccess(object):
    """A data descriptor that sets and returns values
       normally and prints a message logging their access.

    def __init__(self, initval=None, name='var'):
        self.val = initval
        self.name = name

    def __get__(self, obj, objtype):
        print 'Retrieving', self.name
        return self.val

    def __set__(self, obj, val):
        print 'Updating' , self.name
        self.val = val

class MyClass(object):
    x = RevealAccess(10, 'var "x"')
    y = 5

print MyClass.x
MyClass.x = 20
print MyClass.x
MyClass.x = 30
print MyClass.x

Retrieving var "x"

I am at a lost on how to intercept class attribute sets. Can anyone
help :-/


More information about the Python-list mailing list