How to convert (inherit) parent to child class?

后端 未结 5 1623
孤独总比滥情好
孤独总比滥情好 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:17

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

提交回复
热议问题