子クラスにある静的変数を取得するために子クラスが呼び出される親クラスの関数を作成しようとしています。Python:子クラスの静的変数を呼び出す
ここに私のコードです。
class Element:
attributes = []
def attributes_to_string():
# do some stuff
return ' | '.join(__class__.attributes) # <== This is where I need to fix the code.
class Car(Element):
attributes = ['door', 'window', 'engine']
class House(Element):
attributes = ['door', 'window', 'lights', 'table']
class Computer(Element):
attributes = ['screen', 'ram', 'video card', 'ssd']
print(Computer.attributes_to_string())
### screen | ram | video card | ssd
私はそれがself.__class__
を使用して、クラスのインスタンスであれば、私はこれを行うだろうか知っているが、この場合に参照する一切self
はありません。 classmethod
と
インスタンスメソッドは、彼らが静的であれば、HTTPS([ 'staticmethod']がなければならない最初の引数 –
として' self'を持っている必要があり、私たちを与えます://docs.python.org/2/library/functions.html#staticmethod)decorator –
この関数は静的で、 'self'は必要ありません。 – MakPo