I am trying to understand descriptors better.
I don't understand why in the foo method the descriptors
As far as I understand descriptors the
def __init__(self, initval=None, name='var'):
self.val = initval
self.name = name
def __get__(self, obj, objtype):
def __set__(self, obj, val):
self.val = val
x = RevealAccess(10, 'var "x"')
y = 5
self.z = RevealAccess(13, 'var "z"')
m = MyClass()
m.z # no print
m.x # prints var x
z is an attribute on the instance, not on the class. The descriptor protocol only applies to attributes retrieved from a class.
From the Descriptor HOWTO:
For objects, the machinery is in
and in the Implementing Descriptors section of the Python Data Model:
The following methods only apply when an instance of the class containing the method (a so-called descriptor class) appears in an owner class (the descriptor must be in either the owner’s class dictionary or in the class dictionary for one of its parents).
m.z cannot be found in the class dict;
type(m).__dict__['z'] does not exist; it is found in
m.__dict__['z'] instead. Here
m is the instance and the owner class is
z does not appear in the owner class dictionary.