我已经为SQLite编写了一个非常简单的select函数,但是我对如何传递成员函数感到困惑。。。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 * from ?;".replace('?',table).replace("select * ", "select "+select)).fetchone()))
logfile.close()
else: return str(cursor.execute("select * from ?;".replace('?',table).replace("select * ", "select "+select)).fetchone())
如何将此成员函数作为参数传递?
您只需传递
self.fetchone
来传递该函数。在如果您希望它作为默认值,只需在函数定义中使用
None
,然后添加在函数本身中。在
如果您想在另一个对象上调用该方法,但
^{pr2}$self
将其作为字符串传递并使用以下代码(基于您的else
代码,因为该代码较短):lambda可以做到这一点
打印“你好世界”
此技术还可以扩展到处理传递和转换参数
^{pr2}$打印“foo”
您可以使用getattr:
相关问题 更多 >
编程相关推荐