仅继承Python函数的一部分

I have two classes that inherit from the same parent class. Both have a function that is common only in part. For example both print Hello three times, but Daughter1 will print World each time, and Daughter2 will print the counter each time. This is how I am doing it now.

class Mother():
    def foo(self):
        for self.i in range(3):
            print('Hello')
            self.bar()
    def bar(self):
        print('This is just a dummy method')

class Daughter1(Mother):
    def bar(self):
        print('World')

class Daughter2(Mother):
    def bar(self):
        print(self.i)

d1 = Daughter1()
d1.foo()

d2 = Daughter2()
d2.foo()

它可以工作,但是我发现它令人困惑并且容易出错。有一个更好的方法吗?

另一种选择是在子代方法中实现循环。

class Mother():
    def foo(self):
        print('Hello')

class Daughter1(Mother):
    def bar(self):
        for i in range(3):
            self.foo()
            print('World')

class Daughter2(Mother):
    def bar(self):
        for i in range(3):
            self.foo()
            print(i)

d1 = Daughter1()
d1.bar()

这里的问题是真实的循环非常复杂,所以我宁愿只编码一次。