0
クラスのinit()メソッドでインスタンス変数を定義し、そこから新しいクラスを派生させました。セカンダリクラスのインスタンスからこの変数を参照しようとすると、継承されていないようです。Pythonでインスタンス変数を参照する方法
これはコードです:
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'
それは働いていたが、私は何かを(私は推測)に移動、私は何を見つけることができません。 ご協力ありがとう
これは正しいです。しかし、 'type(self)'の代わりに 'self .__ class__'を使ってクラス属性にアクセスする方が正しいと言います。なぜOPがブール値の代わりに文字列 'false'を使用しているのだろうかと思います。 – RobertB
ありがとう、@ロベルトB。私は辞書であるため文字列を使用しています(コードに誤ってインデックスがありません)。文字列以外のデータ型を辞書の値として使用できますか? –
試してみてください。 – RobertB