Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert (inherit) parent to child class?

I would like to know how to convert parent object that was return by some function to child class.

class A(object):
    def __init__():
        pass

class B(A):
    def functionIneed():
        pass

i = module.getObject() # i will get object that is class A
j = B(i) # this will return exception
j.functionIneed()

I cannot change class A. If I could I would implement functionIneed to class A, but it is impossible because of structure of code.

like image 512
Tomas Avatar asked Apr 05 '12 14:04

Tomas


People also ask

What is inherited from the parent class into the child class?

The child class inherits the attributes and functions of its parent class. If we have several similar classes, we can define the common functionalities of them in one class and define child classes of this parent class and implement specific functionalities there.

Can we assign object of parent class to child class?

The parent class can hold reference to both the parent and child objects. If a parent class variable holds reference of the child class, and the value is present in both the classes, in general, the reference belongs to the parent class variable.

Can we typecast parent to child in Java?

However, we can forcefully cast a parent to a child which is known as downcasting. After we define this type of casting explicitly, the compiler checks in the background if this type of casting is possible or not. If it's not possible, the compiler throws a ClassCastException.

Can a child class be inherited?

Inheritance allows us to define a class that inherits all the methods and properties from another class. Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class.


3 Answers

You said you want to implement something like this:

class B(A):
    def functionIneed():
        pass

But really what you would be making is something more like this (unless you had intended on making a class or static method in the first place):

class B(A):
    def functionIneed(self):
        pass

Then you can call B.functionIneed(instance_of_A). (This is one of the advantages of having to pass self explicitly to methods.)

like image 167
wkschwartz Avatar answered Oct 18 '22 19:10

wkschwartz


I have a strong suspicion, nay, conviction, that there is something horribly wrong with your program design that it requires you to do this. In Python, unlike Java, very few problems require classes to solve. If there's a function you need, simply define it:

def function_i_need(a):
     """parameter a: an instance of A"""
     pass # do something with 'a'

However, if I cannot dissuade you from making your function a method of the class, you can change an instance's class by setting its __class__ attribute:

>>> class A(object):
...     def __init__(self):
...         pass
... 
>>> class B(A):
...     def functionIneed(self):
...         print 'functionIneed'
... 
>>> a = A()
>>> a.functionIneed()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'functionIneed'
>>> a.__class__ = B
>>> a.functionIneed()
functionIneed

This will work as long as B has no __init__ method, since, obviously, that __init__ will never be called.

like image 21
A. Jesse Jiryu Davis Avatar answered Oct 18 '22 20:10

A. Jesse Jiryu Davis


Python does not support "casting". You will need to write B.__init__() so that it can take an A and initialize itself appropriately.

like image 25
Ignacio Vazquez-Abrams Avatar answered Oct 18 '22 21:10

Ignacio Vazquez-Abrams