I have been working with inheritance, but I got confused as to why my parent class is not recognizing the init of my sub classes. Here is an example of my code that is giving me none when run, when it shouldn't.
class Coins(object):
def make_change(self, amount):
change = []
for coin in (self._coins):
change.append(amount // coin)
amount = amount - change[-1] * coin
class US_Coins(Coins):
def __init__(self):
self._coins = [50, 25, 10, 5, 1]
class Metric_Coins(Coins):
def __init__(self):
self._coins = [50, 20, 10, 5, 2, 1]
metric = Metric_Coins()
us = US_Coins()
print(us.make_change(73))
print(metric.make_change(73))
coins = Coins()
print(coins.make_change(27))
Metric_Coinswill inherit properties fromCoins. ButCoinswon't inherit anything from anyone. MeaningCoinswon't get any values or properties from any of the other classes. One such variable isself._coinswhich is only created in two of the classes, but never defined inCoins. As @antona pointed out correctly. this shows a basic concept of how this works.return changeto yourmake_changemethod so that it gives you an output. Also, only subclasses can get methods from their parents and not the other way around.Coinswere to use__init__of its subclasses - the two are not compatible. WouldCoins()._coinsbe[50, 25, 10, 5, 1]or[50, 20, 10, 5, 2, 1]?