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

编程入门 行业动态 更新时间:2024-10-27 02:28:28
本文介绍了Python函数告诉我,当我只发送一个参数时,我发送了两个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用Google的 webapp 框架。

我在下面要做的只是发送 query.fetch 转换为一个函数,该函数将获取结果并与它们一起创建一个表。

class Utilities(): def create_table(results):#为结果创建一张表....

变量结果获取两个结果从query.fetch返回

results = query.fetch(10)#返回两个结果 util = Utilities () util.create_table(results)

然后我得到错误

$ b

util.create_table(结果)TypeError: create_table()需要1个参数(给出2个)

我曾经认为 results 会自动通过引用传递。我错了吗?

解决方案

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

<$ p $ (), def create_table(self,results):通过#来得到

应该正常工作:)

编辑: 这也意味着,您可以调用方法也不绑定到一个实例(即obj.fun()):

utils = Utilities() Utilities.create_tables(utils,results)

I'm using Google's webapp framework.

What I'm trying to do below is simply send the results of query.fetch to a function that will take the results and create a table with them.

class Utilities(): def create_table(results): #Create a table for the results....

variable results gets two results back from query.fetch

results = query.fetch(10) #This returns two results util = Utilities() util.create_table(results)

Then I get the error

util.create_table(results) TypeError: create_table() takes exactly 1 argument (2 given)

I had thought that results would automatically get passed by reference. Am I wrong?

解决方案

The first argument is set implicitly by python when the method is bound to an instance. In this case util. When defining a method in a class, the first argument is usually named self and is the bound object.

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

Should work fine :)

Edit: This also means, you can call such methods also when not bound to an instance (i.e. obj.fun()):

utils = Utilities() Utilities.create_tables(utils, results)

更多推荐

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

本文发布于:2023-07-26 11:45:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1216016.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   告诉我   我只   送了   函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!