WhiteRau
WhiteRau

Reputation: 857

Python - printing object error: object is not subscriptable

messing around with Python and was building a simple text cart thing. when i try to iterate through the cart, using the defined __str__, it give me this error: 'CartItem' object is not subscriptable. i've tried to understand what this was talking about, but have failed to find anything on the matter.

here is the relevant code:

class CartItem:
  def __init__(self, name, price):
    self.name = name
    self.price = price
  def __str__(self):
    print(self.name + " --> " + self.price)
    return "-- item printed --\n"

class Cart:
  def __init__(self):
    self.items = []

  def __str__(self):
    if(len(self.items) > 0):
      index = 0
      for item in self.items:
        print(str(index) + ": " + item[index].name + ' --> ' + item[index].price)
        index += 1
    else:
      print("No Items in Cart")
    return "-- cart items printed--\n"

  def addItem(self, cartItem):
    self.items.append(cartItem)
    print (cartItem.name + ", costing $" + cartItem.price + ", has been added to cart\n")
    print(self.items)  # *should* print out the contents of the cart, but doesn't...

flarn = CartItem("flarn", "200.00")
print(flarn)
userCart = Cart()
userCart.addItem(flarn)
print(userCart)  # if i remove this line, i get [<__main__.CartItem object at 0x....>]??

can someone please explain what that error, 'CartItem' object is not subscriptable, means in context of this code? bonus point if you can explain the output removing print(userCart) returns.

Upvotes: 0

Views: 3091

Answers (1)

buran
buran

Reputation: 14263

The problem is item[index].name - CartItem is not subscriptable as the error states. You don't need to use index, you iterate over list. Also you can simplify your __str__() method.

Note that using print() inside the __str__() method is bad. Better construct a string to describe the cart and return it. Also should have __repr__ instead of __str__() for CartItem. It will be used when you print items in container like list Cart.items. Also you can use the string representation of the Item when you construct the Cart.__str__()

class CartItem:
    def __init__(self, name, price):
        self.name = name
        self.price = price
    def __repr__(self):
        return f'{self.name} --> {self.price}'


class Cart:
    def __init__(self):
        self.items = []

    def __str__(self):
        if self.items:
            return '\n'.join(f'{idx}: {item}' for idx, item in enumerate(self.items))
        return "Empty Cart"

    def addItem(self, cartItem):
        self.items.append(cartItem)
        print(f'{cartItem.name}, costing ${cartItem.price}, has been added to cart')
        print(self.items)  # *should* print out the contents of the cart, but doesn't...


userCart = Cart()
print(userCart)
flarn = CartItem("flarn", "200.00")
print(flarn)
userCart.addItem(flarn)
flarn = CartItem("flarn", "100.00")
print(flarn)
userCart.addItem(flarn)
print(userCart)

output:

Empty Cart
flarn --> 200.00
flarn, costing $200.00, has been added to cart
[flarn --> 200.00]
flarn --> 100.00
flarn, costing $100.00, has been added to cart
[flarn --> 200.00, flarn --> 100.00]
0: flarn --> 200.00
1: flarn --> 100.00

Upvotes: 3

Related Questions