Home>
class Test (object):
    def __init __ (self):
        self.test = 0
class Test2 (Test):
    def __init __ (self):
        Test () .__ init __ (self)
    def hoge (self, data):
        print (data)


If i have defined a class like the above, you can add the hoge method of the parent class: Test to the child class: Test2 to the parent class:
Please teach me how to call with Test.

  • Answer # 1

    You can use it if you call.

    class Parent:
        def func_p (self):
            print ('func_p')
            self.func_c (42)
    class Child (Parent):
        def func_c (self, data):
            print ('func_c')
            print (data)
    ch = Child ()
    ch.func_p ()

    However, this design is bad because the parent class depends on the child class.
    If you actually write code like the following, you'll get an error:

    pr = Parent ()
    pr.func_p () # AttributeError: 'Parent' object has no attribute 'func_c'

    There are some workarounds, but it is better to revisit the design itself.

    Addendum:
    When using the parent class as an abstract class,
    In other words, this is not the case if instantiating the Parent itself is incorrect.