Skip to content

Commit

Permalink
extract out getting subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
alxwrd committed Sep 13, 2018
1 parent 012eb47 commit 8b7aa0e
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions coinhandler/coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ class Coin:
def __new__(cls, value):
value *= cls.multiplier # Adjust the value

subclasses = sorted(
Coin.__subclasses__(),
key=lambda c: c.multiplier,
reverse=True)

for klass in subclasses:
for klass in Coin.sub_coins():
# If the classes multiplier is a multiple of the value,
# upgrade the new object to that type.
if value % klass.multiplier == 0:
coin = object.__new__(klass)
coin.value = value
return coin

@classmethod
def sub_coins(cls):
return sorted(
Coin.__subclasses__(),
key=lambda c: c.multiplier,
reverse=True)

@coerce_other
def __eq__(self, other):
return self.value == other
Expand Down

0 comments on commit 8b7aa0e

Please sign in to comment.