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

标签 python google-app-engine

我正在使用 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/

相关文章:

python - 无法在 jupyter 笔记本单元中第二次加入 pandas 数据框

python - 从命令行运行脚本时,忽略 sklearn Gridsearch 中 n_jobs = -1 的警告,不适用于 warnings.simplefilter ('ignore' )

java - 使用 Objectify 查询 Geopt

python - 如何在discord.py(rewrite)中做出 channel 删除 react ?

python - 为什么 Python 的 yield 语句会形成一个闭包?

python - SQLalchemy 以声明方式描述关联对象的正确方法是什么

java - Spring Security - 持久记住我的问题

java - App Engine 1.7 升级时出现 Junit EntityManager NullPointerException

java - Google App Engine Spring Security 登录失败问题

python - 在 Google App Engine 中使用关系数据建模多对多