0

I want to do something in a base class (FooBase) with the class attribues of the derived classes (Foo). I want to do this with Python3.

class BaseFoo:
   #felder = [] doesn't work

   def test():
      print(__class__.felder)

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


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

Maybe there is a different approach to this?

buhtz
  • 10,774
  • 18
  • 76
  • 149
  • 2
    Something's very wrong here. Is `test` supposed to be a *class method* or an *instance method*? It must at the very least accept either `cls` or `self` as argument. – deceze Jul 29 '16 at 09:56

1 Answers1

1

You need to make test a class method, and give it an argument that it can use to access the class; conventionally this arg is named cls.

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

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


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

output

['eins', 'zwei', 'yep']
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182