[英]How to reference an instance variable in 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'
它正在工作,但是我移動了一些東西(我想),但是我找不到。 謝謝你的幫助
在__init__
方法中,必須使用self.attributes
,而不是attributes
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.