Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python class inheritance and sharing variables

I have a question about accessing a variable between two classes, where one is the base class. I have a program which I’m writing where there’s a single ‘settings’ variable and it’s shared between several classes. This is a simplistic version of it.

This code snippet works. When I run it, I can access run() from the Top class, and both classes have access to self.settings, and when I print it out from go(), it contains both ‘a’ and ‘b’ keys.

The problem is, pylint and my IDE say "Instance of ‘Base’ has no ‘settings’ member". Which I understand is because it’s not initialized in Base.init().

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Base:
    def __init__(self):
        self.settings["b"] = False

    def run(self):
        print("RUN")


class Top(Base):
    def __init__(self):
        self.settings = dict(a=True)
        Base.__init__(self)

    def go(self):
        print(self.settings)


x = Top()
x.go()
x.run()

This is a slightly modified version, where I am passing ‘self’ from Top twice to Base, and using ‘main.settings’ instead of self.settings. Pylint nor my IDE complain about this. I’m clearly passing the self instance into it to share, so I understand that.

class Base:
    def __init__(self, main):
        main.settings["b"] = False

    def run(self):
        print("RUN")


class Top(Base):
    def __init__(self):
        self.settings = dict(a=True)
        Base.__init__(self, self)

    def go(self):
        print(self.settings)


x = Top()
x.go()
x.run()

What I don’t understand, is what is the proper way to achieve this? The other option of course is to pass the settings variable itself. However I have several variables and possibly methods which need to be used by both classes, so passing ‘self’ seems like the best option.

>Solution :

Init settings in Base, not Top.

class Base:
    def __init__(self):
        self.settings = {}
        self.settings["b"] = False

    def run(self):
        print("RUN")


class Top(Base):
    def __init__(self):
        Base.__init__(self)
        self.settings["a"] = True

    def go(self):
        print(self.settings)

Child classes should depend on their parent classes, not vice versa. If Base doesn’t init self.settings, then it depends on some other as-yet-undefined class to init it (which is a bad dependency/assumption to introduce).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading