Reputation: 203
Okay, I'm trying to migrate some similar classes underneath a parent to make modifying all the classes at once easier. This is my code:
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)
I have several different styles of syntax on the child credit cards because I'm not sure of the proper syntax for it. When I run this, I get this error:
TypeError: unbound method __init__() must be called with CreditCard instance as first argument (got VisaCreditCard instance instead)
Can anyone help me fix this error? Thank you!
Upvotes: 0
Views: 1610
Reputation: 1122152
You forgot to inherit from CreditCard
:
class VisaCreditCard(CreditCard):
The same applies to your MasterCardCreditCard
and DiscoverCreditCard
classes.
Upvotes: 4