Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,65 @@


class Dessert(object):

def __init__():
def __init__(self,price,calories=None):
# Edit me!
# You need to be able to initialize a Dessert object with arguments:
# price - required
# calories - optional

self.price = price
self.calories = calories
# This should set the object's price and calories, accessible by
# .price and .calories respectively.
pass

# Add a calories_per_dollar method that returns the calories per dollar
# for the dessert.

def calories_per_dollar(self):
if self.calories == None:
return None
else:
ratio = self.calories/self.price
return ratio
# Define a method is_a_cake on Dessert that returns False
def is_a_cake(self):
return False



class Cake(Dessert):

def __init__():
class Cake(Dessert):
def __init__(self,kind):
# Edit me!
# Cakes all cost the same amount and have the same calories, so their
# price and calories can be set at the class-level, not during init.
# However, we need to be able to tell cakes apart. Accept argument:
# kind - required

pass

self.kind = kind
price = 5
calories = 200
super(Cake,self).__init__(price,calories)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to do this, although it works! initializing your parent is mostly done if you really need to call whatever logic is inside its __init__. if it's just a case of creating constant values, you can define them at the class level:

class Cake(Dessert):
    price = 5
    calories = 200

   def __init__(self, kind):
     ... etc ...

then self.price will always be 5 for a Cake, and so on.

# Define a method is_a_cake on Cake that returns True
# (This will override the one on Dessert)
def is_a_cake(self):
return True


class Menu(object):

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):
if isinstance(item, Dessert): ## I don't really understand what's going on here, Jennie
desserts.append(item)
return desserts

def cakes(self):
# Return only the items in self.items which are cakes
cakes = []
for item in self.items:
if isinstance(item, Cake):
cakes.append(item)
return cakes
10 changes: 9 additions & 1 deletion test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,17 @@ def test_object_relationships():
# NOTE: To test that it really works, you probably want to create a Menu
# with a list that includes things that *aren't* desserts, like integers.

assert False # Take this line out, it forces the test to fail
dessert1x = Dessert(price=10)
dessert2x = Dessert(price=12)
dessert3x = Dessert(price=13)
cake1x = 12
cake2x = 13
my_dessertsx = Menu([dessert1x, dessert2x, dessert3x, cake1x, cake2x])
my_menux = my_dessertsx.desserts()
assert len(my_menux) == 3

# Create a cakes() method that does the same thing.

# This code is the test for cakes():

dessert1 = Dessert(price=10)
Expand Down