OiO.lk Blog python Why can't a derived class in Python reference inherited attributes in its definition?
python

Why can't a derived class in Python reference inherited attributes in its definition?


I have this snippet:

class A(object):
    def foo():
        pass

class B(A):
    bar = foo()

As written, this file will fail to run with the error NameError: name 'foo' is not defined. However, since foo is defined in A, and is inherited by B, I should be able to access them.

I know I can simply access foo in the __init__ method of B, but I want to access it in the top-level definition.



You need to sign in to view this answers

Exit mobile version