python - removing an object from a class holding objects -


how can remove object class holding objects?

the question sounds bit confusing. basically, have character class has inventory - list of item objects. 1 of methods in character class involves removing item inventory. know in order have (listname).remove(item), run errors. keep getting "typeerror: 'str' object not callable" if try such

player1.removeinventory(inv[0]) inv list of objects.

here classes items , inventory:

item

class item(object):     def __init__(self, itemtype, value):         self.__itemtype = itemtype         self.__value = value 

to string item

def __str__(self):     out = ""     out += "\ntype: " + self.__itemtype     out += "\nvalue: " + str(self.__value)     return out 

inventory class

class inventory(object):   #aggregation item     def __init__(self, items):     self.__items = items  #a list of items 

to string inventory

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

the inventory argument in class character.

character class

class player(object): #aggregation 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 inventory property

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

method remove item

@inventory.setter def removeinventory(self, item):     self.__inventory.remove(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) print(player1.inventory) player1.removeinventory(inv[0]) print(player1.inventory) 

when create property, , setter function in property, called whenever 1 assignement property. means

@inventory.setter def removeinventory(self, item):     self.__inventory.remove(item) 

method called when there code like: (character).inventory = item - not want (the given item removed instead of added inventory - or become inventory)

instead, call want, player1.removeinventory(inv[0]), leave method undecorated, without @property.(...) decorator.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -