I'm using sqlachemy to connect to MYSQL and need to close connection after each query is executed.
Planning to use class decorators to accomplish this. Here is my brief code.
I get this error:
TypeError: __call__() missing 1 required positional argument: 'args'
class Connection(object):
class close_sqlalchemy_connection(object):
def __init__(self, f):
print("inside myDecorator.__init__()")
self.f = f
def __call__(self, args):
print("inside myDecorator.__call__()", *args )
self.f(args)
print("function decorted")
def __init__(self):
self.mysql_engine = 'mysql_engine' # this will be mysql sqlalchemy connection
@close_sqlalchemy_connection
def execute_query(self):
print('execute_query')
i = Connection()
i.execute_query()
Try using a decorator function, much easier if you don't need additional parameters on setup such as
@decorator(*params)
def method(...):
Here's an example:
from functools import wraps
class Connection(object):
def close_sqlalchemy_connection(f):
@wraps (f)
def inner(*args, **kwargs):
# args[0] is instance itself, if you want to do things with it
return f(*args, **kwargs)
return inner
def __init__(self):
self.mysql_engine = 'mysql_engine' # this will be mysql sqlalchemy connection
@close_sqlalchemy_connection
def execute_query(self):
print('execute_query')
i = Connection()
i.execute_query()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.