我有这段代码用于测试数据描述符
class DataDescriptor(object):
def __init__(self):
self.value = 1990
def __get__(self,obj,cls):
print "__get__ in Data descriptor"
return self.value
def __set__(self,obj,value):
self.obj = value
print "__set__ in Non Data Descriptor"
def __del__(self,obj):
print "__del__ in Non Data Descriptor"
del self.obj
class NonDataDescriptor(object):
def __get__(self,obj,cls):
print "__get__ in Non Data descriptor"
class C(object):
dd = DataDescriptor()
ndd =NonDataDescriptor()
def __init__(self):
self.__value = 1
cobj = C()
cobj.dd
当我执行该操作时,我只能看到打印语句,但看不到我在 get 函数中退出的实际值
我的输出仅此
__get__ in Data descriptor
最佳答案
您没有打印返回值。所以,你没有看到它。
你需要做:-
print cobj.dd
关于Python 数据描述符不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14807506/