pmv
pmv

Reputation: 357

python loop function to loop another function over a list

I have a function called add_ten. I want to loop this function over a list. I want to create a looper function and pass the list and the add_ten function to loop over as input

def add_ten(x):
    y = x+10
    print 'value of x is ' + str(x)
    print 'value of y is ' + str(y)

def looper(list,func):
    for i in list:
        return func(i)

I call the looper function

looper([1,2],add_ten())

I get this error.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-6510e446c709> in <module>()
----> 1 looper([1,2],add_ten())

TypeError: add_ten() takes exactly 1 argument (0 given)

How do I create the looper function properly so that it takes the right arguments?

Upvotes: 0

Views: 182

Answers (5)

AKS
AKS

Reputation: 19811

TypeError: add_ten() takes exactly 1 argument (0 given)

You are getting this error because you are calling the function add_ten() without an argument.


I want to create a looper function and pass the list and the add_ten function to loop over as input.

So just pass the function itself in the looper function without executing it, like following:

looper([1,2], add_ten)

And, then call the function within the for loop with the i value:

def looper(list, func):
    for i in list:
        return func(i)   # <--- notice the parentheses instead of square brackets.

Though, I am not sure about the purpose of return func(i) since it is not returning anything. So, if you just need to print in the add_ten function, you can remove the return from this:

def looper(list, func):
    for i in list:
        func(i)

Upvotes: 1

yaccob
yaccob

Reputation: 1333

With looper([1,2],add_ten()) your're passing the result of the function-call rather than the function itself.

The fixed version looks like this:

def add_ten(x):
    y = x+10
    print 'value of x is ' + str(x)
    print 'value of y is ' + str(y)

def looper(list,func):
    for i in list:
        return func(i)

looper([1,2],add_ten)

Also be aware that your add_ten function always returns None and doesn't have any effect but printing the original and the incremented value. A compact way of actually returning an amended list could look like this:

def add_ten(x):
    return x + 10

def looper(list,func):
    return [ func(i) for i in list ]

result = looper([1,2],add_ten)
print result

Upvotes: 0

Dilettant
Dilettant

Reputation: 3335

Trying to understand the question, I arrive at the following working code:

#! /usr/bin/env python
from __future__ import print_function


def add_ten(x):
    print('value of x is %s\nvalue of y is %s' % (str(x), str(x + 10)))


def looper(list, func):
    for i in list:
        func(i)

if __name__ == '__main__':
    looper([1, 2], add_ten)

Output is on my machine:

value of x is 1
value of y is 11
value of x is 2
value of y is 12

Hope this helps in learning Python ;-)

Note in the implementation of looperyou for sure do not want to return early in the first iteration, as then only the first list element is processed with a result. Here above I just call it and the side effect of the print or whatever is defined in add_ten "happens" ...

Upvotes: 1

Jasper
Jasper

Reputation: 3947

  1. Dont call the function when it's an argument

looper([1,2], add_ten)

  1. Use parentheses instead of squaree brackets when calling func(i).

Otherwise,your code looks good.

Upvotes: 0

Artur Opalinski
Artur Opalinski

Reputation: 1092

Look closer at the error you get: "add_ten() takes exactly 1 argument (0 given)".

This is because in return func[i] you should do: return func(i) (note different brackets).

Otherwise the argument to the call to funct() is lacking.

Upvotes: 0

Related Questions