I need some help in understanding below problem in python3.6. I've 2 classes: parent & child. child inherits parent. I need to access the variables declared/passed in child within parent class.
But when i run the command :
Command: child(fmt='abc').test_func()
Observations:
Note: **kwargs is intended in child class because it needs to accept variable inputs at runtime, which works.
TIA
========================================================================
class parent:
def __init__(self, fmt = 'X'):
self.fmt = fmt
self.mode = 'abc'
========================================================================
class child(parent):
def __init__(self, **kwargs):
super().__init__()
def test_func(self):
return self.fmt
========================================================================
Step1 -- hand the variable 'abc' to the child
child(fmt='abc').test_func()
# this works, the variables goes into the child's __init__ function
Step2 -- the child should hand it up to parent
def __init__(self, **kwargs):
# the variable is here
super().__init__()
# but never gets passed or assigned anywhere
Step3 -- the parent assigns it
def __init__(self, fmt = 'X'):
# your input of 'abc' never reaches here
self.fmt = fmt
Two possible solutions. Either
__init__
method, now the variable will pass directly to the parent's __init__
method skipping Step2 entirely.__init__
method, by forwarding any supplied variables (so super().__init__(**kwargs)
)