Python function telling me that I sent two arguments when I only sent one

I am using google 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....

the variable resultsreturns two results from query.fetch

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

Then i get an error

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

I thought it resultswould be automatically passed by reference. Am I mistaken?

+3
source share
5 answers

python, . . self .

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

:)

Edit:
, , (.. Obj.fun()):

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

:

def create_table(self, results):

, self , , !

+3

, , . .

def create_table(self, results):
+1

. get (self), .

class Utilities(): 
    def create_table(self, results): 
+1

The first argument of each method of the python class must be an instance of the class itself. By convention self, this is a keyword that is used to indicate an instance of a class. There is no reason to use a different keyword, and you should not stick to the agreement self:). When a class method is defined, the argument selfmust be included; however, when the class method is used, the argument is selfimplicitly present.

Example:

class C:
def cMethod(self, a1, a2):
    pass
[...]

>>> cinstance = C()
>>> cinstance.cMethod(x1, x2)

I just wanted to point out two aspects :). Bye

+1
source

Source: https://habr.com/ru/post/1779289/


All Articles