我正在使用 Google 的 webapp框架。
下面我要做的只是发送 query.fetch 的结果到一个函数,它将获取结果并用它们创建一个表。
class Utilities():
def create_table(results):
#Create a table for the results....
变量 results
从 query.fetch 获取两个结果
results = query.fetch(10) #This returns two results
util = Utilities()
util.create_table(results)
然后我得到错误
util.create_table(results) TypeError: create_table() takes exactly 1 argument (2 given)
我原以为 results
会自动通过引用传递。我错了吗?
最佳答案
当方法绑定(bind)到实例时,第一个参数由 python 隐式设置。在这种情况下,实用程序。在类中定义方法时,第一个参数通常命名为 self
并且是绑定(bind)对象。
class Utilities():
def create_table(self, results):
pass # more to come
应该可以正常工作:)
编辑:
这也意味着,您也可以在未绑定(bind)到实例时调用此类方法(即 obj.fun()):
utils = Utilities()
Utilities.create_tables(utils, results)
关于Python 函数告诉我当我只发送一个参数时我发送了两个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4393340/