Why Is There No `items_in_cart` Parameter?


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

Why do we not initialize items_in_cart as in the following:

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

Do dictionaries simply not need to be explicitly called in the arguments of the __init__()
function or?


FAQ: Learn Python - Introduction to Bitwise Operators - It's Not All Animals and Fruits
#2

Hi @arnodunstatter,

The self and customer_name variables are passed into the __init__() function so it can use those values from outside. self.customer_name is a different variable than customer_name, though.

We want to be able to pass in a customer name, since that will be different each time, but the initial state of the cart will never change, so the function doesn’t need to take an argument for that.