How to convert (inherit) parent to child class?

后端 未结 5 1615
孤独总比滥情好
孤独总比滥情好 2020-12-09 22:59

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         


        
5条回答
  •  时光取名叫无心
    2020-12-09 23:29

    How about:

    i = module.getObject() # i will get object that is class A
    try:
        i.functionIneed()
    except AttributeError:
        # handle case when u have a bad object
    

    Read up on duck typing.

提交回复
热议问题