0

I have a function called from within a function like this:

def counter(number):
  number = number + 1
  return number

def iterator(iteration, function):
  for i in range(iteration):
    mfunction = function
    output = mfunction()
  return output

I want to call it something like this:

number = 0
number = iterator(5, partial(counter, number))
print number

This returns 1, where as it should return 5, because the count function should have been called 5 times.
I realize that somehow the data isn't outputting correctly but I can't figure out how to return out of the for loop.

This question may seem redundant, because I could easily do something like:

for i in range(5):
  number = counter(number)

But the latter example defeats the purpose of this program.

I think the problem is that I need to create an argument in the counter function to account for the iterator function. But the problem in my actual program is that I would have to modify many functions to do this, and I am trying to avoid that.

I am not that familiar with calling functions inside of functions and any help would be greatly appreciated,

7
  • 1
    number = number + 1 in counter function does change only the parameter number, not the number outside the function. Commented Nov 10, 2013 at 4:50
  • partial(counter, number) is equivalent to partial(counter, 0). So the code is calling counter(0) 5 times. Commented Nov 10, 2013 at 4:52
  • Yeah, I just don't know how to get that number out Commented Nov 10, 2013 at 4:52
  • well it prints as 1, so the number is affected. It just doesn't repeat in the for loop. Commented Nov 10, 2013 at 4:53
  • It is affected by number = iterator(..). (After the iterator function return = all 5 iteration is done. not after individual call of counter) Commented Nov 10, 2013 at 4:55

1 Answer 1

2

partial(counter, number) is equivalent to partial(counter, 0). So the code is calling counter(0) 5 times.

def counter(number):
    number = number + 1
    return number

def iterator(iteration, function, arg):
    for i in range(iteration):
        arg = function(arg)
    return arg

number = 0
number = iterator(5, counter, number)
print number # => 5

def counter(number, delta):
    number = number + delta
    return number

def iterator(iteration, function, *args):
    args = list(args)
    for i in range(iteration):
        ret = function(*args)
        args[0] = ret
    return ret

number = 0
number = iterator(5, counter, number, 5)
print number # => 25
Sign up to request clarification or add additional context in comments.

7 Comments

This works but the number of arguments is variable which is why I used partial.
@NickSimas, Are the number of arguments and the number of return value's items always equal?
The return is always 1 value. The arguments vary.
@NickSimas, You can use Arbitrary Argument Lists. Or you can pass a tuple or a list and use Unpacking Argument list
@NickSimas, But that doesn't make sense. counter receives the return value of itself as parameter.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.