I searched quite a bit on this, but can't seem to find anything specific to my problem.
Say I have three separate python files fileA.py, fileB.py, and fileMain.py
fileB.py inherits from fileA.py.
fileA.py
class Parent():
def __init__(self):
self.valueA = 5
fileB.py
from fileA import Parent
class Child(Parent):
def __init__(self):
Parent.__init__():
self.valueB = 10
def Calculate(self):
self.result = self.valueB + self.valueA
print(self.result)
And in fileMain.py I have some code that calls upon the Calculate method in the Child class AFTER valueA in the Parent class has been changed.
from fileA import Parent
from fileB import Child
class MainProgram():
def __init__(self):
self.parent = Parent()
self.child = Child()
self.parent.valueA = 8
self.child.Calculate()
foobar=MainProgram()
My problem is, that the printed output is 15 and not 18. Why? How could I resolve this to get the expected result?
The reason I want for fileB.py to inherit from fileA.py is that I plan on having a fileC.py also inherit from fileA.py and have other calculations done there also using self.valueA. The valueA is supposed to be set by the user of the program. I simplified things here. Hope this makes sense?
Sorry if my use of terminology is wrong. Please correct me. :)
You seem to not be clear on how classes
and instances
(also referred to as objects
) work.
Parent
, Child
and MainProgram
are classes
. You can think of them as blueprints. They do not change.
When you instantiate a class, you get an instance
, e.g:
parent = Parent()
child = Child()
parent
and child
are instances. When you call parent.valueA = 8
you are changing a value on the instance
called parent
.
When you call child.Calculate()
it has nothing to do with the parent
instance. When you instatiated Child()
the Parent()
constructor was called and so child.valueA
was set to equal 5
. You would need to call child.valueA = 8
in order to change it.
For more information, consider reading through this: https://realpython.com/python3-object-oriented-programming/
(EDIT: By the way you refer to fileB inhereting from fileA and so forth. This an incorrect use of terminology. A file does not inherit from another file. You would say a module imports from another module. A class inherits from another class)
EDIT2:
If your goal is to share the state
of Parent
between all it's instances, you can use a Borg pattern for it. Essentially, every instance of the Borg shares its state with all other instances. So if you do parent1 = Parent()
and parent2 = Parent()
, and then do parent1.valueA = 8
, then parent2.valueA
will also equal 8
.
It works like this:
class Parent(object): #This is a Borg class
__shared_state = {}
def __init__(self):
self.__dict__ = self.__shared_state
self.valueA = 5
class Child(Parent):
def __init__(self):
Parent.__init__(self)
self.valueB = 10
def Calculate(self):
self.result = self.valueB + self.valueA
print(self.result)
class MainProgram():
def __init__(self):
self.parent = Parent()
self.child = Child()
self.parent.valueA = 8
self.child.Calculate()
foobar=MainProgram()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With