Error when trying to pass list into function.

E

erikcw

Hi,

I'm getting the following error when I try to pass a list into a
function.

My List: crea =[(u'218124172', u'536', u'32394'), (u'218320282',
u'1323', u'77931')]

Traceback (most recent call last):
File "wa.py", line 118, in ?
curHandler.walkData()
File "wa.py", line 49, in walkData
self.results[parent][child]['results'] = self.calculate(crea)
#pass in list of tuples
TypeError: calculate() takes exactly 1 argument (2 given)

def calculate(dta):
#stub

How can I make this work?

Thanks!
Erik
 
K

kyosohma

Hi,

I'm getting the following error when I try to pass a list into a
function.

My List: crea =[(u'218124172', u'536', u'32394'), (u'218320282',
u'1323', u'77931')]

Traceback (most recent call last):
File "wa.py", line 118, in ?
curHandler.walkData()
File "wa.py", line 49, in walkData
self.results[parent][child]['results'] = self.calculate(crea)
#pass in list of tuples
TypeError: calculate() takes exactly 1 argument (2 given)

def calculate(dta):
#stub

How can I make this work?

Thanks!
Erik

You need to do this:

def calculate(self, dta):
# stub


Because when you call a method in a class with this:
self.calculate(crea), you pass in an instance of the object (self) AND
the list.

Mike
 
S

Stargaming

erikcw said:
Hi,

I'm getting the following error when I try to pass a list into a
function.

My List: crea =[(u'218124172', u'536', u'32394'), (u'218320282',
u'1323', u'77931')]

Traceback (most recent call last):
File "wa.py", line 118, in ?
curHandler.walkData()
File "wa.py", line 49, in walkData
self.results[parent][child]['results'] = self.calculate(crea)
#pass in list of tuples
TypeError: calculate() takes exactly 1 argument (2 given)

def calculate(dta):
#stub

How can I make this work?

Thanks!
Erik
The first argument to a method (a function bound to an instance) is
always the instance itself. (I assume you *have* a method here because
you call self.calculate(crea), what will itself call
SomeClass.calculate(self, crea).)

For further information, consult the python tutorial
http://docs.python.org/tut/node11.html#SECTION0011400000000000000000.

HTH,
Stargaming
 
B

Bruno Desthuilliers

erikcw a écrit :
Hi,

I'm getting the following error when I try to pass a list into a
function.

My List: crea =[(u'218124172', u'536', u'32394'), (u'218320282',
u'1323', u'77931')]

Traceback (most recent call last):
File "wa.py", line 118, in ?
curHandler.walkData()
File "wa.py", line 49, in walkData
self.results[parent][child]['results'] = self.calculate(crea)
#pass in list of tuples
TypeError: calculate() takes exactly 1 argument (2 given)

def calculate(dta):
#stub

How can I make this work?

In Python, the first argument of an instance method is the instance on
which it has to operate [1]. IOW, your problem is with the definition of
calculate(). You want:

class DontKnowWhat(object):
def calculate(self, data):
# code here

[1] you don't have to pass this argument - this will be handled by
Python - but you need to declare it.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top