[Tutor] Class Inheritance
Rafael Knuth
rafael.knuth at gmail.com
Tue Feb 21 04:49:23 EST 2017
Hey there,
I am trying to wrap my head around Class Inheritance in Python, and I
wrote a little program which is supposed to calculate revenues from
customers who don't get a discount (parent class) and those who get a
30% discount (child class):
class FullPriceCustomer(object):
def __init__(self, customer, rate, hours):
self.customer = customer
self.rate = rate
self.hours = hours
print ("Your customer %s made you %s USD this year." %
(customer, rate * hours))
class DiscountCustomer(FullPriceCustomer):
discount = 0.7
def calculate_discount(self, rate, hours):
print ("Your customer %s made you %s USD at a 30% discount
rate this year." % (self.customer, self.rate * rate * discount))
customer_one = DiscountCustomer("Customer A", 75, 100)
customer_two = FullPriceCustomer("Customer B", 75, 100)
The DiscountCustomer class instance gets me the wrong result (it does
not calculate the discount) and I was not able to figure out what I
did wrong here.
Can anyone help? Thanks!
All the best,
Rafael
More information about the Tutor
mailing list