Python函数告诉我,当我只发送一个参数时,我发送了两个参数 [英] Python function telling me I sent two arguments when I only sent one
问题描述
我使用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函数告诉我,当我只发送一个参数时,我发送了两个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!