variable de classe, d'instance et __get__
bonjour,
ci dessous un petit bout de code.
je ne comprends pas pourquoi j'arrive à déclenché le descripteur __get__ en variable de classe et pas en variable d'instance
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
class Prop(object):
def __get__(self, instace, owner):
return "Prop __get__"
class Model1(object):
a=Prop()
class Model2(object):
def __init__(self):
self.a = Prop()
testModel1 = Model1()
testModel2 = Model2()
print testModel1.a
print testModel2.a
#>>> Prop __get__
#>>><__main__.Prop object at 0x0000000002718EF0> |
pourtant, d'apres la doc :
Citation:
The starting point for descriptor invocation is a binding, a.x. How the arguments are assembled depends on a:
Direct Call
The simplest and least common call is when user code directly invokes a descriptor method: x.__get__(a).
Instance Binding
If binding to a new-style object instance, a.x is transformed into the call: type(a).__dict__['x'].__get__(a, type(a)).
Class Binding
If binding to a new-style class, A.x is transformed into the call: A.__dict__['x'].__get__(None, A).
il me semble que je suis dans le cas de l'instance binding, mais j'avoue que je peux avoir des ( grosses ) lacunes en anglais.
Merci pour votre aide