removing an object from a class holding objects

How can I remove an object from a class holding objects?

The question sounds a bit confusing. Basically, I have a character class which has an inventory – a list of item objects. One of my methods in the character class involves removing an item from the inventory. I know in order to do that I just have to do (listname).remove(item), however I run into errors. I keep getting “TypeError: ‘str’ object is not callable” if I try something such as

where inv is a list of objects.

Here are my classes for items and inventory:


class Item(object):
    def __init__(self, itemType, value):
        self.__itemType = itemType
        self.__value = value

To string for item

def __str__(self):
    out = ""
    out += "nType: " + self.__itemType
    out += "nValue: " + str(self.__value)
    return out


class Inventory(object):   #Aggregation from item
    def __init__(self, items):
    self.__items = items  #A list of items

To string for inventory

def __str__(self):
    out = ""
    out += "nItems: "
    for x in self.__items:
        out += str(x)
    return out

The inventory is an argument in the class for a character.

Character Class

class Player(object): #Aggregation from Inventory
    def __init__(self, name, age, gender, playerClass, HP, ATK, DEF, inventory, gold = 0, level = 1, defending = False):
    self.__level = level
    self.__name = name
    self.__age = age
    self.__gender = gender
    self.__playerClass = playerClass
    self.__HP = HP
    self.__ATK = ATK
    self.__DEF = DEF
    self.__inventory = inventory ##list of items...###
    self.__gold = gold
    self.__defending = defending

getting the inventory with a property

def inventory(self):
    number = 1
    out = ""
    for item in self.__inventory:
        out += str(item)
        number +=1
    return out

Method to remove an item

def removeInventory(self, item):

The code I’ve tried running

i1 = Item("Health potion", 1)
i2 = Item("Health potion", 1)
inv = [i1,i2]
player1 = Player("Brandon",19,"Male","Wizard", 100, 15, 10, inv, 200)

Source: oop

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.