Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting name of subclass from superclass?

I have a base class bc and a number of subclasses based on bc.

class bc(Object):
  def get_subclass_name(self):
      # Retrieve the name of the most derived subclass of this instance
      pass
  def somemethod(self):
      x = self.get_subclass_name()

class sc1(bc):
    pass

class sc2(bc)
    pass

The idea is that when somemethod() is invoked on an instance of a subclass of bc, it will be able to use the name of the most derived subclass of that instance without needing to know in advance what potential subclasses may exist.

I have put together a test case for this:

class base_class(object):
    @classmethod
    def get_subclass_name(cls):
        return cls.__name__

    def somemethod(self):
        print(base_class.get_subclass_name())

class sub_class(base_class):
    pass

sub_class().somemethod()

When this code is run it produces base_class rather than sub_class.

like image 819
Jonathan Avatar asked Oct 12 '13 15:10

Jonathan


People also ask

How do you get the subclass method from superclass?

Yes its possible to call sub class methods using super class by type casting to sub class object . By type casting super class object to sub class object we can access all corresponding sub class and all super class methods on that reference.

How do you find the class name for a subclass?

The Class object has a getName() method that returns the name of the class. So your displayClass() method can call getClass(), and then getName() on the Class object, to get the name of the class of the object it finds itself in.

How do you define a subclass from a superclass?

Definition: A subclass is a class that derives from another class. A subclass inherits state and behavior from all of its ancestors. The term superclass refers to a class's direct ancestor as well as all of its ascendant classes.

What is getClass () getName () in Java?

Java Class getName() Method The getName() method of java Class class is used to get the name of the entity, and that entity can be class, interface, array, enum, method, etc. of the class object. Element Type.


1 Answers

You need a class method.

class bc(Object):
    @classmethod
    def get_subclass_name(cls):
        return cls.__name__

    def somemethod(self):
        x = self.get_subclass_name()

Normal methods, when invoked, get passed the instance as the first parameter. This is why you see self everywhere in Python.

When you invoke a class method, however, the concrete class of the instance is passed to the function. We usually use the name cls, as I have done here.

The classic use case for class methods is alternative constructors. For example, the standard library's dict class provides a fromkeys class method, which constructs a new dict from the supplied keys and a single value. You could construct such a dict manually, but the Python developers made life easy for you!

like image 199
Benjamin Hodgson Avatar answered Sep 19 '22 18:09

Benjamin Hodgson