必须使用CreditCard实例作为第一个参数调用未绑定的方法__init __()(获取VisaCreditCard实例)

时间:2014-07-14 07:45:17

标签: python python-2.7

好的,我正在尝试迁移父类下面的一些类似的类,以便更容易地修改所有类。这是我的代码:

class CreditCard():
    def __init__(self, name, short, tag, length):
        self.name = name
        self.short = short
        self.tag = tag
        self.length = length
        self.CCnumber = tag
        while (len(self.CCnumber) < self.length - 1):
            rand_int = random.randrange(10)
            rand_str = str(rand_int)
            self.CCnumber = self.CCnumber + rand_str
        if (length == 15):   
            d = CheckSumDigit(self.CCnumber)
            self.CCnumber = self.CCnumber + d
        if (not RigorousVerifyLuhn(self.CCnumber)):
            ln = self.length - 1
            clip = self.CCnumber[0:ln]
            fulfilled = False
            dig = 0
            while (dig <= 9 and fulfilled == False):
                cand = clip + str(dig)
                if (RigorousVerifyLuhn(cand)):
                    fulfilled = True
                    self.CCnumber = cand
                dig = dig + 1
            if (fulfilled == False):
                if (len(self.CCnumber) != self.length):
                    print("Invalid " + self.name + " number, LENGTH " + len(self.CCnumber) + " (" + self.CCnumber + ")")
                else:
                    print("Invalid " + self.name + " number, LUHN " +  "(" + self.CCnumber + ")")



class AmexCreditCard(CreditCard):

    def __init__(self):
        self.NAME = 'American Express'
        self.SHORT = 'AMEX'
        self.TAG = '3'
        self.LENGTH = 15
        CreditCard.__init__(self, 'American Express', 'AMEX', '3', 15)

class VisaCreditCard():

    def __init__(self):
        self.NAME = 'Visa'
        self.SHORT = 'VISA'
        self.TAG = '4'
        self.LENGTH = 16
        CreditCard.__init__(self, self.NAME, self.SHORT, self.TAG, self.LENGTH)

class MasterCardCreditCard():

    def __init__(self):
        CreditCard.__init__(self, 'MasterCard', 'MC', '5', 16)

class DiscoverCreditCard():

    def __init__(self):
        CreditCard.__init__(self, 'Discover', 'DISC', '6011', 16)

我在子信用卡上有几种不同的语法风格,因为我不确定它的正确语法。当我运行它时,我收到此错误:

TypeError: unbound method __init__() must be called with CreditCard instance as first argument (got VisaCreditCard instance instead)

任何人都可以帮我修复此错误吗?谢谢!

1 个答案:

答案 0 :(得分:4)

您忘记继承CreditCard

class VisaCreditCard(CreditCard):

这同样适用于您的MasterCardCreditCardDiscoverCreditCard类。