如何在Python中引用实例变量

2024-04-18 16:28:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我在类的init()方法中定义了一个实例变量,并从中派生了一个新类。当我试图从第二个类的实例引用这个变量时,它似乎没有被继承。你知道吗

代码如下:

company=Company()
person=Person()
task=Task()

print(company.get_attrs())

class Entity(Persistent):
    list_of_attrs=[]
    list_of_attrs_flag=False

    def __init__(self):
        attributes={}
        attributes_edition_flag={}
        if not type(self).list_of_attrs_flag:
            type(self).list_of_attrs=self.get_attrs_from_persistent()
            type(self).list_of_attrs_flag=True
            for attr in type(self).list_of_attrs:
                attributes[attr]=''
                attributes_edition_flag[attr]='false'

    def get_attrs(self):
        return(self.attributes)

class Company(Entity):
    def hello(self):
        print('hello')

这是我得到的错误:

MacBook-Pro-de-Hugo:Attractora hvillalobos$ virtual/bin/python3 control.py
Traceback (most recent call last):
  File "control.py", line 7, in <module>
    print(company.get_attrs())
  File "/Users/hvillalobos/Dropbox/Code/Attractora/model.py", line 48, in get_attrs
    return(self.attributes)
AttributeError: 'Company' object has no attribute 'attributes'

它在工作,但我移动了一些东西(我猜),但我找不到什么。 谢谢你的帮助


Tags: ofinpyselfgetdeftypeattrs