简体   繁体   中英

Python Calling Child's Method from Parent Class

I'm working with yapsy to setup some runtime plugins for a project. I've derived my own Plugin type from the IPlugin abstract class:

class MyPlugin(IPlugin):
    def __init__(self):
        # call IPlugin's constructor, to initialize is_activated, activate()
        # and deactivate().
        super(self.__class__, self).__init__()
        self.__is_installed = self.installed

    @property
    def installed():
        # this must be implemented in the plugin
        raise NotImplementedError("installed must be implemented by %s" % self)

And the plugin looks like this:

from mypackage.plugin import MyPlugin

class TestPlugin(MyPlugin):
    @property
    def installed():
        return False

The call to self.installed calls the installed method from MyPlugin and not the overridden installed method from TestPlugin . I've been looking through all kinds of documentation on python inheritance and super(), but I'm not seeing (or understanding) what I need. Any suggestions?

Edit: I wrote up a quick example to confirm that things work the way I though they did. The child method should be called "automatically" instead of the parent's method. Since it's not, I assume it must be something that yapsy is doing. Still trying to figure it out.

class Parent(object):
    def __init__(self):
        self.name = 'parent'
        print('%s\t\t Parent.__init__()' % self.name)

class Child(Parent):
    def __init__(self):
        self.name = 'child'

        # super doesn't overriding name
        print('%s\t\t Child.__init__()\t before super()' % self.name)
        super(Parent, self).__init__()
        print('%s\t\t Child.__init__()\t after super()' % self.name)

        # this does override name
        print('%s\t\t Child.__init__()\t before Parent.__init__()' % self.name)
        Parent.__init__(self)
        print('%s\t\t Child.__init__()\t after Parent.__init__()' % self.name)

        self.set_name()

    def set_name(self):
        print('%s\t\t Child.__init__()\t before Child.set_name()' % self.name)
        self.name = 'child'
        print('%s\t\t Child.__init__()\t after Child.set_name()' % self.name)


class Grandchild(Child):
    # when creating an instance of grandchild, this method is called and
    # Parent.set_name is not.
    def set_name(self):
        print('%s\t\t Grandchild.__init__()\t before Grandchild.set_name()' % self.name)
        self.name = 'grandchild'
        print('%s\t Grandchild.__init__()\t after Grandchild.set_name()' % self.name)


if __name__ == '__main__':
    p = Parent()
    c = Child()
    g = Grandchil

I was looking at this and my solution was this

Base plugin class

class PluginBase():

        def generate(self):
             print('PluginBase plugin(generate)')

        def generated_files(self):
            print('PluginBase plugin(generated files)')
            return {}

Actual plugin

from PluginBase import PluginBase
from yapsy.IPlugin import IPlugin

class Test(PluginBase, IPlugin):

    def generate(self):
         print('Test plugin(generate)')

    def generated_files(self):
        print('Test plugin(generated files)')
        return {'file1': 'some/path/to/file1',
                'file2': 'some/other/path/to/file2'}

So inherit IPlugin directly in your actual plugin class rather than the ancestor class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM