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?

1
  • 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. Commented Jul 29, 2016 at 9:56

1 Answer 1

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']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.