1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python updating a subclass attribute [closed]

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm learning to work with classes in Python and I can't understand why my subclass attribute is not being overwritten.

    If I do char.info.name = "SOME" then everything works and the name attribute changes, but if I try to change the char.inv.items attribute from the char.inv class method then it just doesn't change.

    Or rather I call char.inv.update() from char.inv.addItem() expecting that addItem() will change the base and update() will update everything in char.inv.items from the base.

    def update(self, invDBObject):
    self.items = []
    inv = list(filter(lambda lst: lst[1] == self.charID, invDBObject))
    for item in inv:
    self.items.append(self.Item(self.charID, item[0], item[2]))


    Is there something I'm doing wrong or something I don't know?

    class Character:
    def __init__(self, discordID, charID, invDBObject):
    self.inv = self.Inv(charID, invDBObject)
    class Inv:
    def __init__(self, charID:int, invDBObject):
    self.charID = charID
    self.items = []
    self.update(invDBObject)

    def addItem(self, itemID, count):
    global Database_Conn;Database_Conn.ping(reconnect=True)
    DC = Database_Conn.cursor()

    insertInDB = DC.execute(f"INSERT INTO inv VALUES ({itemID}, {self.charID}, {count});")
    Database_Conn.commit()

    invDBObject = DC.execute(f"SELECT * FROM inv;")
    invDBObject = DC.fetchall()
    self.update(invDBObject)

    def update(self, invDBObject):
    self.items = []
    inv = list(filter(lambda lst: lst[1] == self.charID, invDBObject))
    for item in inv:
    self.items.append(self.Item(self.charID, item[0], item[2]))
    def get(self, id:int):
    for item in self.items:
    if item.id == id:
    return item
    return None

    class Item:
    def __init__(self, charID, itemID, count):
    self.charID = charID
    self.id = itemID
    self.count = count
    def write(self):
    global Database_Conn;Database_Conn.ping(reconnect=True)
    DC = Database_Conn.cursor()
    DC.execute(f"UPDATE inv SET count = {self.count} WHERE charID = {self.charID} and itemID = {self.id};")
    Database_Conn.commit()


    class CharactersHandler:
    def __init__(self):
    self.characters = []
    for player in tqdm(players):
    self.characters.append(Character(player[0], player[1], invDBObject))
    def find(self, charID=None):
    if charID is not None:
    else:
    for char in self.characters:
    if str(char.charID) in str(search).split(sep=","):
    else: return char


    characters = CharactersHandler()


    A function that attempts to change a character's object.

    def editCharStats(infoType):
    global characters
    char = characters.find(int(charID))
    itemObj = char.inv.get(int(itemID))
    if (itemObj is None) and (int(value)>0):
    char.inv.addItem(itemID, value)


    When update() is called from __init__ on startup, everything works.

    Continue reading...

Compartilhe esta Página