Answer the question
In order to leave comments, you need to log in
How to call parent method from python variable?
from libs.ConfigManager import ConfigManager
file times, test.py
class TestMe():
config = ConfigManager()
def runme(self):
print('alive')
asdf = TestMe()
asdf.config.test()
file two, libs /ConfigManager.py
class ConfigManager:
def test(self):
super().runme()
expected result:
AttributeError: 'super' object has no attribute 'runme'
question, how to call a method from parent TestMe from ConfigManager?
Answer the question
In order to leave comments, you need to log in
You have TestMe not the parent of ConfigManager, but the owner of the config property (which is an instance of the ConfigManager class).
In order for TestMe to be a parent of ConfigManager, ConfigManager must be declared like this:
Then it will be possible to call runme () as a method of the ConfigManager itself
class ConfigManager(TestMe):
def test(self):
self.runme()
class ConfigManager(TestMe):
def runme(self):
super(self).runme()
class TestMe():
config = ConfigManager(self)
def runme(self):
print('alive')
class ConfigManager():
def __init__(self, owner):
self.owner = owner # запоминаем хозяина
def test(self):
self.owner.runme() # вызываем метод хозяина
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question