0

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))
3
  • 1
    Inheritance works upwards, not downwards. Meaning Metric_Coins will inherit properties from Coins. But Coins won't inherit anything from anyone. Meaning Coins won't get any values or properties from any of the other classes. One such variable is self._coins which is only created in two of the classes, but never defined in Coins. As @antona pointed out correctly. this shows a basic concept of how this works. Commented Nov 20, 2018 at 6:33
  • 2
    You should add a return change to your make_changemethod so that it gives you an output. Also, only subclasses can get methods from their parents and not the other way around. Commented Nov 20, 2018 at 6:43
  • Can you clarify what your problem is? A parent class is not meant to do anything with methods of its subclasses. Even in your case, it would not make sense if Coins were to use __init__ of its subclasses - the two are not compatible. Would Coins()._coins be [50, 25, 10, 5, 1] or [50, 20, 10, 5, 2, 1]? Commented Nov 20, 2018 at 10:17

1 Answer 1

1

You need to define self._coins field in Coins class. Without it, function make_change cannot be executed because it evokes field that does not exist in this parent class (for coin in (self._coins)).

Edited: in order to achieve your goal you need to create a field inside Coins class:

class Coins(object):

    def __init__self():
        self._coins = []

    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))

Now you are able to operate with make_change method on the us and metric; and also on coins object.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, is there any way to account for the changing self._coins ([50, 25, 10, 5, 1] and [50, 20, 10, 5, 2, 1]) in the Coins class?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.