Python, sharing mysql connection in multiple functions - pass connection or cursor? -
i opening mysql connection in main function , use connection in multiple functions called main function.
is there wrong passing cursor main function instead of passing connection?
i.e.:
pass in cursor main function
def main(): conn = pymysql.connect(...) conn cursor: func1(cursor) func2(cursor) conn.close() def func1(cursor): cursor.execute('select ...') def func2(cursor): cursor.execute('insert ...')
pass in connection main function
def main(): conn = pymysql.connect(...) func1(conn) func2(conn) conn.close() def func1(conn): conn cursor: cursor.execute('select ...') def func2(conn): conn cursor: cursor.execute('insert ...')
the answer comes law of demeter: pass cursor.
this leads shorter code. in case, it's pretty trivial, may lot (e.g., passing database name vs. passing cursor).
Comments
Post a Comment