【发布时间】:2012-10-13 19:01:03
【问题描述】:
这两个代码会给出两个不同的输出,为什么?
class Test:
def __get__(self, instance, owner):
return 42
def __set__(self, instance, value):
pass
class A:
a = Test()
a = A()
print(a.a) // print 42
a.a = 0
print(a.a) // print 42
和
class Test:
def __get__(self, instance, owner):
return 42
def __set__(self, instance, value):
pass
class A:
pass
a = A()
a.a = Test()
print(a.a) // print <__main__.Test object at 0xb700d6cc>
a.a = 0
print(a.a) // print 0
Python引擎中的属性是如何存储的?
【问题讨论】:
标签: python descriptor