python调用子类函数_如何使子类在所有子类中调用函数

在Python中,作者遇到子类调用特定函数的问题。基类BaseA的entery_point()方法应调用从A或A2派生的子类的entery_point_b()方法。错误在于self._children的初始化位置。正确实现是在__init__中初始化,确保仅包含直接子类。修复后,代码按预期工作。
摘要由CSDN通过智能技术生成

我在确定使“子类”在其子类中调用函数的最佳方法时遇到了一些问题

设计看起来像这样:

class BaseA():

a_attr = 'I handle Table'

def entery_point(self):

print('Computing Value from %s' % self.a_attr)

for child in self._children: #filling the self._children is where i think need help

child().entery_point_b()

class BaseB(A):

b_attr = 'I handle Fields'

def entery_point_b(self):

print('Computing Value from %s and %s' % (self.a_attr,self.b_attr) )

#Now the concrete classes

class A(BaseA):

a_attr = 'table_name'

class B(BaseB,A):

b_attr = 'field_name_1'

class BB(BaseB, A):

b_attr = 'field_name_2'

#another set of concrete classes to handle another table

class A2(BaseA):

a_attr = 'another_table_name'

class B2(BaseB,A2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值