How to convert (inherit) parent to child class?

18,359

Solution 1

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

Solution 2

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.

Solution 3

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.)

Share:
18,359
Tomas
Author by

Tomas

Updated on June 04, 2022

Comments

  • Tomas
    Tomas about 2 years

    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.

  • dcrosta
    dcrosta about 12 years
    I just blogged about an alternative solution that might be better, or might be far, far worse. Messing with __class__ is weird and possibly dangerous, but it seems to work (in CPython at least) -- see A Python "Cast Constructor"
  • Tobias Kienzler
    Tobias Kienzler about 11 years
    @dcrosta Would you mind turning that post into an answer? You know, linkrot etc
  • Tahlor
    Tahlor about 6 years
    So if I want to inherit from another class, and want the ability to convert the parent class to the child, is this right? Or am I in the wrong paradigm. class B(A): def __init__(self, a_instance): a_instance.__class__ = B
  • Wtower
    Wtower over 2 years
    OP has an existing obj from parent class that needs to have the child function functionIneed(), not a brand new obj from child class.