What Python rule allows you to find a handle first?

I ran into last night and I still don't understand how to explain this:

class Foo(object): @property def dave(self): vars(self)['dave'] = 1 return 2 f = Foo() print f.dave print f.dave 

Running this code calls:

 2 2 

The question is why? My understanding of access to attributes is that the instance dictionary is checked before the class dictionary, and the dictionary of any databases, however, as seen above, the instance dictionary is not displayed before the descriptor is in the class dictionary.

+2
source share
1 answer

My understanding of attribute access is that the instance dictionary is checked before the class dictionary, and the dictionary of any reason

Data descriptors are an exception:

For example, bindings, the priority of the descriptor call depends on which descriptor methods are defined. Typically, data descriptors define both __get__() and __set__() , while descriptors without data only have the __get__() method. Data descriptors always override the override in the instance dictionary. In contrast, data descriptors without data can be overridden by instances.

http://docs.python.org/reference/datamodel.html#invoking-descriptors

+5
source

All Articles