python - Passing a member function as a function arg? -
i've written simple select function sqlite, i'm confused @ how pass member function... e.g.: .fetchone()
, .fetchmany()
.
def select(cursor, select="*", table="reuters", fetch=".fetchone()", tologfile=false, logfile=""): if tologfile: logfile = open(logfile, 'w') logfile.write(str(cursor.execute("select * ?;".replace('?',table).replace("select * ", "select "+select)).fetchone())) logfile.close() else: return str(cursor.execute("select * ?;".replace('?',table).replace("select * ", "select "+select)).fetchone())
how pass member function arg?
you can pass self.fetchone
pass function.
if want default value use none
in function definition , add
if whatever none: whatever = self.fetchone
in function itself.
if want call method on object self
keep passing string , use code (based on else
code since one's shorter):
result = self.execute("select * ?;".replace('?',table).replace("select * ", ("select "+attr))) return str(getattr(result, whatever)())
Comments
Post a Comment