如何在Python3中访问基类中派生类的类属性?

时间:2016-07-29 09:51:35

标签: python python-3.x class-attributes

我想在基类(FooBase)中使用派生类(Foo)的类属性执行某些操作。我想用Python 3

来做这件事
class BaseFoo:
   #felder = [] doesn't work

   def test():
      print(__class__.felder)

class Foo(BaseFoo):
   felder = ['eins', 'zwei', 'yep']


if __name__ ==  '__main__':
    Foo.test()

也许有不同的方法吗?

1 个答案:

答案 0 :(得分:1)

您需要将Sub CopyArray() Dim x As Variant Dim y As Variant x = Array(1, 2, 3) y = Application.Transpose(x) '<~~ Transpose it For i = LBound(y) To UBound(y) Debug.Print y(i, 1) Next i End Sub 设为class method,并为其提供一个可用于访问该类的参数;通常这个arg名为test

cls

<强>输出

class BaseFoo:
    @classmethod
    def test(cls):
        print(cls.felder)

class Foo(BaseFoo):
    felder = ['eins', 'zwei', 'yep']


if __name__ ==  '__main__':
    Foo.test()