有這樣的代碼:爲什麼__get__方法不被實例屬性調用?
class A:
def __init__(self, x):
self.x = x
def __get__(self, obj, type=None):
print("__get__")
return self.x
def __set__(self, obj, value):
pass
class B:
a_oc = A(44)
def __init__(self, y):
self.a_ob = A(y)
b = B(3)
print(b.a_oc) # class attribute called __get__
print(b.a_ob) # __get__ not called
對於類屬性__get__
被調用,比如屬性事實並非如此。爲什麼?
你確定嗎?我只是運行這個代碼,而沒有'__get__'打印到我的控制檯。 –
我用Python 3.3試過它,它是 – scdmb
啊,NM,我能夠在Python 3.2中複製它,但不是Python 2.7 –