Skip to content

Instantly share code, notes, and snippets.

@tuannvm
Created January 14, 2017 16:26
Show Gist options
  • Save tuannvm/9ea23fa0eb0a017a4647e15fd45c3d4b to your computer and use it in GitHub Desktop.
Save tuannvm/9ea23fa0eb0a017a4647e15fd45c3d4b to your computer and use it in GitHub Desktop.
class Store:
def __init__(self, name):
self.name = name
self.items = []
def add_item(self, name, price):
self.items.append({
'name': name,
'price': price
})
def stock_price(self):
total = 0
for item in self.items:
total += item['price']
return total
@classmethod
def franchise(cls, store):
cls = store
return "{} - franchise".format(cls.name)
# Return another store, with the same name as the argument's name, plus " - franchise"
@staticmethod
def store_details(store):
#pass
return "{}, total stock price: {}".format(store.name, store.stock_price())
# Return a string representing the argument
# It should be in the format 'NAME, total stock price: TOTAL'
@jslvtr
Copy link

jslvtr commented Jan 15, 2017

Hey!

In the franchise method, you have two arguments: cls, a reference to the class (i.e. cls and Store are the same thing); and store, a Store object that has a name and potentially a list of items.

However, in line 20 you assign cls = store, which means cls is no longer a reference to the class.

The method is meant to "Return another store", which means it must return a Store object. In line 21, you return a string.

In order to return a Store object with the correct name, you should change line 21 to this:

return Store("{} - franchise".format(cls.name))

If you delete line 20, then you can use cls instead of Store (and you'll have to access store.name instead of cls.name):

return cls("{} - franchise".format(store.name))

@tuannvm
Copy link
Author

tuannvm commented Jan 16, 2017

Thanks for the detailed explanation, @jslvtr!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment