import psycopg2
class User:
def __init__(self, database):
self.conn = psycopg2.connect(database)
self.cursor = self.conn.cursor()
def set_address(self, address, user_id):
with self.conn:
self.cursor.execute('UPDATE users SET address=%s WHERE user_id=%s', (user_id, address))
self.conn.commit()
Конструкция with кажется предполагает закрытие соединения с БД
Cursors can be used as context managers: leaving the context will close the cursor.