Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend class method

Tags:

python

Both SubClassA and SubClassB are the sub-classes of Base class (both inherit from same Base class). Since getInfo() method is declared within Base() class it (this method) is shared between both A and B sub-classes. When getInfo() method is called it returns the value of self.attr variable (shared among A and B sub-classes). Now I want to "extend" this method. So when it is called via subClassA instance using instanceA.getInfo() in addition to self.attr it would return self.attrA as well. Likewise if instanceB is used: instanceB.getInfo() self.attrB is returned in addition to returning self.attr. How Base() class's method getInfo() can be extended so it is "customized" for both sub-classes A and B?

class Base(object):
    def __init__(self):
        super(Base, self).__init__()
        self.attr='Attr'

    def getInfo(self):
        info=self.getAttr()
        return info
    def getAttr(self):
        return self.attr

class SubClassA(Base):
    def __init__(self):
        super(SubClassA, self).__init__()  
        self.attrA='attrA'      
    def getAttrA(self):
        return self.attrA

class SubClassB(Base):
    def __init__(self):
        super(SubClassB, self).__init__()  
        self.attrB='attrB'      
    def getAttrB(self):
        return self.attrB

instanceA=SubClassA()
instanceB=SubClassB()
print instanceA.getInfo()
print instanceB.getInfo()
like image 657
alphanumeric Avatar asked Aug 23 '14 04:08

alphanumeric


People also ask

Can a method extend a class?

No. You can extend the class and override just the single method you want to "extend".

How do you extend classes?

Definition and Usage The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.

How do you extend a base class in Python?

Use the super() Function After Extending a Class in Python When there is a new init() inside a child class that is using the parent's init() method, then we can use the super() function to inherit all the methods and the properties from the parent class.

How do you extend a class in C#?

You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself.


1 Answers

Simply define the getInfo method in the subclasses. If desired, use super (as you are in your constructors) to get the result of the base class getInfo() and incorporate it as desired.

To elaborate: when given an instance c of some class C, and asked to look up an attribute c.attr, Python looks for the attribute in a sequence of places. In particular, it will look in derived classes before base classes. Thus the following code

class Base(object):
    def method(self):
        return "I'm a base object!"

class Derived(Base):
    def method(self):
        return "I'm a derived object!"

obj = Derived()
print obj.method()

will print "I'm a derived object!", because Python looks for method in Derived, finds it, and never checks in Base.

You mentioned a need to call Base.method in your Derived object. That's where super comes in. super(Derived, self).method() will find and call Base.method on self. You are already doing that in your constructors.

For example:

class Computer(object):
    def boot_message(self):
        return 'I am a computer'

class AppleComputer(Computer):
    def boot_message(self):
        return super(AppleComputer, self).boot_message() + ' with a really shiny logo'

We avoid duplicating the effort in Computer.boot_message by taking its result and modifying it as necessary.

Note that the super(Base, self).__init__() line is not necessary; in general such calls are only desirable if the base class __init__() does work that the derived class __init__() wants to accomplish without code duplication, which is not the case for object.

like image 162
onethreeseven Avatar answered Oct 22 '22 02:10

onethreeseven