-2

I have a question that sounds perhaps strange, but I want to call a function of my parent class inside my children.

I use tkinter and MyChild allready inherrit from a frame.

ex:

class MyParent:
    def __init__(self):
        do_things()

    def myfunction(self):
        child_class = MyChild()

    def call_me(self):
        print("I'm here!")

class MyChild:
    def __init__(self):
        do_things()

    def my_call(self):
        #here call the call_me function

here I want that when in MyChild class the my_call function is called, it calls the call_me function of the MyParent class. I just want to know if and how it is possible to call call_me in the my_call function like Myparent.call_me()

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
my_name
  • 87
  • 1
  • 1
  • 11
  • Is `MyChild` supposed to be a subclass of `MyParent`? – khelwood Oct 02 '18 at 09:21
  • Possible duplicate of [Call a parent class's method from child class in Python?](https://stackoverflow.com/questions/805066/call-a-parent-classs-method-from-child-class-in-python) – Tristo Oct 02 '18 at 09:22
  • @ksbg The problem is that I use tkinter and I have inherited from a Frame – my_name Oct 02 '18 at 09:23
  • @my_name Python supports multiple inheritance so inheriting from both `Frame` and `MyParent` is not technically an issue (just make sure that `MyParent` inherits from `object` if using Python 2.7.x) - BUT from a design point of view, chances are that what you really want is composition/delegation instead. – bruno desthuilliers Oct 02 '18 at 09:31
  • @my_name In your example, `MyChild` does NOT inherit from `MyParent`. To do so, you have to declare the class that way: `class MyChild(MyParent):` – Antwane Oct 02 '18 at 09:32
  • @Antwane I know, but I removed the tkinter part, because I have 500+ lines of code, but can I do child_class = MyChild(self)? – my_name Oct 02 '18 at 09:36
  • Have a look at e.g. https://www.python-course.eu/python3_inheritance.php The code you show has two completely unrelated classes... – planetmaker Oct 02 '18 at 09:59
  • @planetmaker The `MyChild` class is created in the `myfunction` function, And the `MyChild` class calls the `call_me` function on button click for exemple. – my_name Oct 02 '18 at 10:08
  • You seem to be asking about _inheritance_, but the code uses _composition_. Please provide a [mcve] that accurately reflects the problem you're having. If it's truly about inheritance (child inherits from parent), then create an example that uses inheritance. – Bryan Oakley Oct 02 '18 at 11:54
  • @BryanOakley The problem is that I am new to Python, and don't exactly know what to use. I just want to call the function of my parent from my child without recreating an object. – my_name Oct 02 '18 at 13:49
  • Well, it's unclear what you mean by _parent_ and _child_ - your question says one thing, your code says another. We need to understand whether _child_ is a subclass of _parent_ or not, because it affects the answer. – Bryan Oakley Oct 02 '18 at 14:03

1 Answers1

0

Based on the code in your question, your child will have to be told what its parent is. This is normally done when you create the child. The child can keep a reference, so that it can be used whenever it needs to call a method on the parent.

Example:

class MyParent:
    def myfunction(self):
        child_class = MyChild(parent=self)

    def call_me(self):
        print("I'm here!")

class MyChild:
    def __init__(self, parent):
        self.parent = parent
        do_things()

    def my_call(self):
        self.parent.call_me()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685