Skip to content

Instantly share code, notes, and snippets.

@jennielees
Created May 28, 2015 22:57
Show Gist options
  • Save jennielees/0751d30897fc83037583 to your computer and use it in GitHub Desktop.
Save jennielees/0751d30897fc83037583 to your computer and use it in GitHub Desktop.
Solution to objects
""" Objects models file.
Edit the class definitions to make the tests pass.
"""
class Dessert(object):
def __init__(self, price, calories=None):
self.price = price
self.calories = calories
def calories_per_dollar(self):
if self.calories:
return self.calories / self.price
def is_a_cake(self):
return False
class Cake(Dessert):
price = 5
calories = 200
def __init__(self, kind):
self.kind = kind
def is_a_cake(self):
return True
class Menu(object):
def __init__(self, items):
self.items = items
def desserts(self):
# Return only the items in self.items which are desserts
desserts = []
for item in self.items:
if isinstance(item, Dessert):
desserts.append(item)
return desserts
def cakes(self):
# Return only the items in self.items which are desserts
cakes = []
for item in self.items:
if item.is_a_cake():
cakes.append(item)
return cakes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment