Python - 缺少位置参数

时间:2015-06-08 09:15:44

标签: python

我的Python代码包含以下类

class payment_calculations:
    def __init__(self):
        self.__interestdatecurrent = str()
        return None

    def paymentfixed(self, notional, datecouponstart, datecouponend, interestratecurrent, interestratefixed, datecurrent):

当我用

打电话时
fixedpayment = payment_calculations.paymentfixed(interestnotional, datecoupon1, datecoupon2, c, interestratefixed, interestdatecurrent)

我看到以下堆栈跟踪

 File "C:\Python34\mod_payment_calculation.py", line 83, in coupondatesfixed
    fixedpayment = payment_calculations.paymentfixed(interestnotional,datecoupon1,datecoupon2,c,interestratefixed,interestdatecurrent)
TypeError: paymentfixed() missing 1 required positional argument: 'datecurrent'

我从测试文件调用时已经确认这是有效的。但是,当我从模块本身内部运行时,它会失败。有什么想法吗?

1 个答案:

答案 0 :(得分:5)

您需要创建该类的实例

pc = payment_calculations()
fixedpayment = pc.paymentfixed(interestnotional, datecoupon1, datecoupon2, c, interestratefixed, interestdatecurrent)

否则您正在调用未绑定的函数,并且不会自动提供self参数。相反,interestnotional值转到self参数,datecoupon1转换为notational参数等等。