So I'm trying to dynamically set properties to a class with different names where each property makes a unique call to my database. However, when I access the property objects, all of them return the same result despite looking up on different columns with completely different values each. Here is the code:
class Configs:
def __init__(self, guild_id):
self.guild_id = guild_id
for x in configs:
def fget(self):
return cur.execute(f'SELECT {x.name} FROM configs WHERE guild_id = ?', (self.guild_id,)).fetchone()[0], x.name
def fset(self, value):
cur.execute(f'UPDATE configs SET {x.name} = ? WHERE guild_id = ?', (value, self.guild_id))
con.commit()
setattr(Configs, x.name, property(fget, fset))
The configs variable is a list of objects where each object has a name
attribute which points to a string. The result is always the one that the last element of the configs array would produce, I suspect this is happening because x.name
is used to make the calls and once the for loop is done, x remains as the last element of the array.