is it possible to have a single update method in a class that takes a variable as input for the name of the class variable to be updated.
for example. I have a class that mimics a mysql table and I want a generic update function that updates both the object and the mysql table
However obviously the last line isn't valid for updating the object is there a way that you can do that? Or will I need a function for each element.
class MyClass(object):
def __init__(self, a, b, c, db_id):
self.a = a
self.b = b
self.c = c
self.db_id = db_id
def update_field(self, field, value, cnx):
update_query = "update my_table set %s = %s where id = %s"
cursor = cnx.cursor(buffered=True)
cursor.execute(update_query, (field, value, self.db_id))
**self.field = value**
Thanks