我使用Google的 webapp 框架。
我在下面要做的只是发送 query.fetch 转换为一个函数,该函数将获取结果并与它们一起创建一个表。
class Utilities(): def create_table(results):#为结果创建一张表....变量结果获取两个结果从query.fetch返回
results = query.fetch(10)#返回两个结果 util = Utilities () util.create_table(results)然后我得到错误
$ butil.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 comeShould 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函数告诉我,当我只发送一个参数时,我发送了两个参数
发布评论