我有一个类 Record,用于包含读取文本文件的结果。该文件包含一个带有字段和标签的简单数据库。我想让每个 Record 实例只具有与其数据库关联的属性。基本上:
R1 = Record("file1")
R2 = Record("file2")
print(R1.TI) #"Record 1's title"
print(R2.TI) #AttributeError: 'Record' object has no attribute 'TI'
不幸的是,有些字段可能需要大量处理才能返回有用的东西,而这些值可能永远不需要。所以我希望在第一次调用它们时确定该值,而不是在初始化对象时确定。
因为我只知道标签名称,所以我尝试过:
class tagWrapper(object):
def __init__(self, tag):
self.tag = tag
self.data = None
def __get__(self, instance, owner):
if self.data == None:
try:
#tagToFunc is a dictionary that maps tags to their processing function
self.data = tagToFunc[self.tag](instance._rawDataDict[self.tag])
except KeyError: #I do not know the full list of tags
self.data = instance._rawDataDict[self.tag]
return self.data
class Record(object):
def __init__(self, file):
#Reading file and making _rawDataDict
setattr(self, tag, tagWrapper(tag))
这会导致 R1.TI
生成包装器对象而不是我想要的值。所以我怀疑我用 get 方法搞砸了。
注意:我正在尝试使属性成为单个类实例的一部分并且直到需要时才进行评估。我可以实现一个或另一个,但无法确定如何同时执行这两个操作。
最佳答案
你正在实现描述符协议(protocol),描述符属于类而不是类的实例,所以你不能将它分配给实例属性。
class Tag(object):
def __init__(self, tag):
self.tag = tag
self.data = None
def __get__(self, instance, owner):
if not instance: # if accessed with the class directly, ie. Record.T1, just return this descriptor
return self
if self.data is None:
print "Reading data"
self.data = range(10)
return self.data
class Record(object):
T1 = Tag('T1')
关于python - 动态惰性类属性python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30586700/