python 继承 超类_python:继承超类__init__

I have a base class with a lot of __init__ arguments:

def BaseClass(object):

def __init__(self, a, b, c, d, e, f, ...):

self._a=a+b

self._b=b if b else a

...

All the inheriting classes should run __init__ method of the base class.

I can write a __init__() method in each of the inheriting classes that would call the superclass __init__, but that would be a serious code duplication:

def A(BaseClass):

def __init__(self, a, b, c, d, e, f, ...):

super(A, self).__init__(a, b, c, d, e, f, ...)

def B(BaseClass):

def __init__(self, a, b, c, d, e, f, ...):

super(A, self).__init__(a, b, c, d, e, f, ...)

def C(BaseClass):

def __init__(self, a, b, c, d, e, f, ...):

super(A, self).__init__(a, b, c, d, e, f, ...)

...

What's the most Pythonic way to automatically call the superclass __init__?

解决方案super(SubClass, self).__init__(...)

Consider using *args and **kw if it helps solving your variable nightmare.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值