10. It's not all Animals and Fruit: Clas Variable Issue


Items_in_cart = {} is set as a class variable, therefore any products added will be shared by all ShoppingCart instances. What is the proper placement of items_in_cart = {} (seems like if we place it under a method then it'll become an instance variable and also fail).

class ShoppingCart(object):
    """Creates shopping cart objects
    for users of our fine website."""
    items_in_cart = {}
    def __init__(self, customer_name):
        self.customer_name = customer_name

    def add_item(self, product, price):
        """Add product to the cart."""
        if not product in self.items_in_cart:
            self.items_in_cart[product] = price
            print product + " added."
            print product + " is already in the cart."

    def remove_item(self, product):
        """Remove product from the cart."""
        if product in self.items_in_cart:
            del self.items_in_cart[product]
            print product + " removed."
            print product + " is not in the cart."

my_cart = ShoppingCart('Target')
my_cart.add_item('cheese', 4.20)


If you create a variable in a method then it only exists while the method is running.

Do you mean that each instance should have its own cart? If so, set it as an attribute of the instance


Yep that's my question @ionatan. I'm unsure where in the code to set items_in_cart = {} as an attribute. I've tried placing it under the init method but receive an attribute error.


The default code establishes items_in_cart as a class variable. The instructions do not ask the user to change that.

In my opinion, it would be better design for each ShoppingCart instance to have its own items_in_cart dictionary. If you wish to alter the code to implement this, the submission correctness test (SCT) will let you get away with it. Remove this from its original location ...

items_in_cart = {}

... and place this inside the __init__ method ...

self.items_in_cart = {}

Leave the __init__ method's parameter list as is.


Ah thank you @applypye your answer is appreciated; I was overthinking it.

Here is another approach I wrote:

def __init__(self, customer_name, items_in_cart = None):
         if items_in_cart is None:
             items_in_cart = {}
         self.items_in_cart = items_in_cart
         self.customer_name = customer_name


Note that not every instance variable needs to be initialized using a parameter of the __init__ method. If a particular instance variable will always start out with the same initial value, that value can simply be assigned to the instance variable within the __init__ method without there being a corresponding parameter.

In the approach that you wrote with this header ...

def __init__(self, customer_name, items_in_cart = None):

... a user who chooses to instantiate a non-empty ShoppingCart would need to pass a dict with items already in it as a second argument when creating that instance.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.