python - 动态惰性类属性python

标签 python class oop properties

我有一个类 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/

相关文章:

python - swig 3.0.1、python 3 和 bool 数据类型

javascript - 如何在没有 module.exports 的情况下在 Javascript 中导出 ES6 类

c# - 覆盖或隐藏部分类中的属性

PHP 扩展类没有覆盖函数?

python - 反向排序python中的二维numpy数组

python - 如何编写输出到多个屏幕的视频播放脚本?

python - Seaborn OS X : seaborn. pairplot() ValueError: 'transform' must be an instance of 'matplotlib.transform.Transform'

c++ - 类模板出现在类成员访问表达式中

ios - 将您的自定义类命名为与 Swift/iOS 中的文件名相同的命名约定是否是一个好的命名约定

c++ - fatal error LNK1169 : one or more multiply defined symbols found in game programming