python子类构造函数_python子类

I currently have a class called Polynomial, The initialization looks like this:

def __init__(self, *termpairs):

self.termdict = dict(termpairs)

I'm creating a polynomial by making the keys the exponents and

the associated values are the coefficients. To create an instance of this class, you enter as follows:

d1 = Polynomial((5,1), (3,-4), (2,10))

which makes a dictionary like so:

{2: 10, 3: -4, 5: 1}

Now, I want to create a subclass of the Polynomial class called Quadratic. I want to call the Polynomial class constructor in the Quadratic class constructor, however im not quite sure how to do that. What I have tried is:

class Quadratic(Polynomial):

def __init__(self, quadratic, linear, constant):

Polynomial.__init__(self, quadratic[2], linear[1], constant[0])

but I get errors, anyone have any tips? I feel like I'm using incorrect parameters when I call the Polynomial class constructor.

解决方案

You probably want

class Quadratic(Polynomial):

def __init__(self, quadratic, linear, constant):

Polynomial.__init__(self, (2, quadratic), (1, linear), (0, constant))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值