Python get child class inside parent class static/class method

Bradley Marques

The output of:

class Dog():
    def get_class():
        return __class__

class Cat():
    def get_class():
        return __class__

print(Dog.get_class())
print(Cat.get_class())

is:

<class '__main__.Dog'>
<class '__main__.Cat'>

I want to DRY up my code with a subclass. But the output of:

class BaseClass():
    def get_class():
        return __class__

class Dog(BaseClass):
    pass

class Cat(BaseClass):
    pass

print(Dog.get_class())
print(Cat.get_class())

is

<class '__main__.BaseClass'>
<class '__main__.BaseClass'>

How do I change the code in the second case to obtain the same output as the first case?

baskettaz

you are almost there :

class BaseClass:
    @classmethod
    def get_class(cls):
        return cls

class Dog(BaseClass):
    pass


class Cat(BaseClass):
    pass

print(Dog.get_class())
print(Cat.get_class())

<class '__main__.Dog'>
<class '__main__.Cat'>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

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

From Dev

Call method from parent class inside child class

From Dev

Get child class name in parent method

From Dev

Calling a child method in a parent class in Python

From Dev

OOP Python: Call child class method in Parent class method

From Dev

Get variable from parent class for use in method of child class

From Dev

How to get Parent class variable value in child class construct method

From Java

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

From Dev

Initializing a child class with the magic method __init__ of the parent class in python

From Dev

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

From Javascript

How to call parent method inside a javascript child class

From Dev

import child class inside parent class in javascript

From Java

Parent wrapping child class method

From Dev

Call parent method in child class

From Dev

I want to call parent class method which is overridden in child class through child class object in Python

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 call a parent class method inside a child class method of the same name?

From Dev

Arduino use Parent class Method at child class

From Dev

Call parent class method (with this context) in child class

From Dev

Executing a method from a parent class in a child class

From Dev

How to get Child class in inherited parent method in Ruby?

From Dev

PHP - Get the child class method name in the parent scope

From Dev

Call child method class in parent with parent instance

From Dev

python - remain in base class when calling parent method from child

From Dev

Python How to call child class method from Parent metaclass

From Dev

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

From Java

Get the calling parent class in a static child class

From Dev

Is there a way to get name of child class in parent class?

From Dev

Get the Child Class Propert in Parent class

Related Related

  1. 1

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

  2. 2

    Call method from parent class inside child class

  3. 3

    Get child class name in parent method

  4. 4

    Calling a child method in a parent class in Python

  5. 5

    OOP Python: Call child class method in Parent class method

  6. 6

    Get variable from parent class for use in method of child class

  7. 7

    How to get Parent class variable value in child class construct method

  8. 8

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

  9. 9

    Initializing a child class with the magic method __init__ of the parent class in python

  10. 10

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

  11. 11

    How to call parent method inside a javascript child class

  12. 12

    import child class inside parent class in javascript

  13. 13

    Parent wrapping child class method

  14. 14

    Call parent method in child class

  15. 15

    I want to call parent class method which is overridden in child class through child class object in Python

  16. 16

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

  17. 17

    How to call a parent class method inside a child class method of the same name?

  18. 18

    Arduino use Parent class Method at child class

  19. 19

    Call parent class method (with this context) in child class

  20. 20

    Executing a method from a parent class in a child class

  21. 21

    How to get Child class in inherited parent method in Ruby?

  22. 22

    PHP - Get the child class method name in the parent scope

  23. 23

    Call child method class in parent with parent instance

  24. 24

    python - remain in base class when calling parent method from child

  25. 25

    Python How to call child class method from Parent metaclass

  26. 26

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

  27. 27

    Get the calling parent class in a static child class

  28. 28

    Is there a way to get name of child class in parent class?

  29. 29

    Get the Child Class Propert in Parent class

HotTag

Archive