Python 函数告诉我,当我只发送一个参数时,我发送了两个参数

2024-01-31

我正在使用谷歌的webapp http://code.google.com/appengine/docs/python/tools/webapp/框架。

我下面想做的只是发送结果查询.fetch http://code.google.com/appengine/docs/python/datastore/queryclass.html#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(结果) 类型错误: create_table() 恰好需要 1 论据(给出 2 个)

我本来以为results将自动通过引用传递。我错了吗?


当方法绑定到实例时,第一个参数由 python 隐式设置。在这种情况下,util.在类中定义方法时,第一个参数通常命名为self并且 是绑定对象。

class Utilities():
    def create_table(self, results):
         pass # more to come

应该可以正常工作:)

Edit:
这也意味着,在未绑定到实例时也可以调用此类方法(即 obj.fun()):

utils = Utilities()
Utilities.create_tables(utils, results)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 函数告诉我,当我只发送一个参数时,我发送了两个参数 的相关文章

随机推荐