How to access parent class instance method from child class static method in python?

VVK kumar
class P(object):

    def __init__(self):
        print('Parent')

    @staticmethod
    def M1():
        print('parent Static')

    @classmethod
    def M2(cls):
        print('parent class method')

    def M3(self):
        print('Instance Method')


class Q(P):

    @staticmethod
    def W1():
       super(Q,Q).M3()##Here I am getting error

Q.W1()

TypeError: unbound method M3() must be called with Q instance as first argument (got nothing instead)

Juan Leni

P.M3 is not static or a class method.

Notice the self in the signature of the method:

def M3(self):

There is no way you can call it from W1 without having an instance of a P object.

What you are trying to do it similar to P.M3() and that will not work.

From a Q staticmethod, you can call other static/class methods in your base class, however, to call an instance method you need an instance. A static method in Q does not provide an instance, so it will be unable to call instance methods in the base class.

There are many ways that you could use to call M3 but they will depend on what you really need. For instance:

class Q(P):
    @staticmethod
    def W1():
        p = P()
        p.M3()

    @staticmethod
    def W2(p):
        p.M3()


Q.W1()

some_p = P()
Q.W2(some_p)

some_q = Q()
Q.W2(some_q)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to access method of parent class with instance of child class in c#

From Dev

How to access a child class method from a parent class?

From Dev

es6 access static method of child class from static method of parent?

From Dev

Python get child class inside parent class static/class method

From Java

How to call a Parent Class's method from Child Class in Python?

From Dev

Access child's __class__ from a parent's method in Python

From Dev

Return a new instance of Child class from base class static method

From Dev

Access to property defined in child class from static method in the parent class - javascript

From Dev

getting child class from static method of parent class in Java

From Dev

How to access parent class instance attribute from child class instance?

From Java

How to access parent class variable in child class inside a child method?

From Dev

How to clone a child class instance within a parent class method

From Dev

How can a child class inherit a class method from its parent that gets a class variable from the child in python?

From Dev

How to access the init variable obtained from parent class into child method in python?

From Dev

Call child method class in parent with parent instance

From Dev

How Call child method from parent class

From Dev

Python How to call child class method from Parent metaclass

From Dev

How can I tell a function of parent class is called by classmethod or instance method from child class?

From Dev

Why can I get access to static method of my super class from child instance?

From PHP

Instance child class in abstract static method

From Dev

How to create a child class in TypeScript using parent static method?

From Dev

How to call parent static method using child class in kotlin?

From Dev

How to create an instance of concrete class from static method of abstract class

From Dev

Executing a method from a parent class in a child class

From Dev

How to Implement Parent Class Method From Child Class Methods

From Dev

How to assign a decorator to a method of each child class of a parent class, in Python

From Dev

How to Access Parent class variable in Python and Call Parent Method using Child Object

From Java

Static method cannot access instance members of a class

From Dev

Access child class variable with parent method

Related Related

  1. 1

    how to access method of parent class with instance of child class in c#

  2. 2

    How to access a child class method from a parent class?

  3. 3

    es6 access static method of child class from static method of parent?

  4. 4

    Python get child class inside parent class static/class method

  5. 5

    How to call a Parent Class's method from Child Class in Python?

  6. 6

    Access child's __class__ from a parent's method in Python

  7. 7

    Return a new instance of Child class from base class static method

  8. 8

    Access to property defined in child class from static method in the parent class - javascript

  9. 9

    getting child class from static method of parent class in Java

  10. 10

    How to access parent class instance attribute from child class instance?

  11. 11

    How to access parent class variable in child class inside a child method?

  12. 12

    How to clone a child class instance within a parent class method

  13. 13

    How can a child class inherit a class method from its parent that gets a class variable from the child in python?

  14. 14

    How to access the init variable obtained from parent class into child method in python?

  15. 15

    Call child method class in parent with parent instance

  16. 16

    How Call child method from parent class

  17. 17

    Python How to call child class method from Parent metaclass

  18. 18

    How can I tell a function of parent class is called by classmethod or instance method from child class?

  19. 19

    Why can I get access to static method of my super class from child instance?

  20. 20

    Instance child class in abstract static method

  21. 21

    How to create a child class in TypeScript using parent static method?

  22. 22

    How to call parent static method using child class in kotlin?

  23. 23

    How to create an instance of concrete class from static method of abstract class

  24. 24

    Executing a method from a parent class in a child class

  25. 25

    How to Implement Parent Class Method From Child Class Methods

  26. 26

    How to assign a decorator to a method of each child class of a parent class, in Python

  27. 27

    How to Access Parent class variable in Python and Call Parent Method using Child Object

  28. 28

    Static method cannot access instance members of a class

  29. 29

    Access child class variable with parent method

HotTag

Archive