A
A
Anastasia_K2018-02-01 00:27:21
Python
Anastasia_K, 2018-02-01 00:27:21

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

1 answer(s)
S
Sly_tom_cat ., 2018-02-01
@Anastasia_K

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()

seper() is only needed when you want to call an overridden parent method.
class ConfigManager(TestMe):

    def runme(self):
        super(self).runme()

If you want exactly as you do (when ConfigManager is the value of a property of the TestMe class, then you need to pass the owner of this property as a parameter to the ConfigManager constructor, then inside the ConfigManager you can call the owner method.
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 question

Ask a Question

731 491 924 answers to any question