Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of next() in Python

I am trying to use the next function on an iterator, however, I have a local variable in the same scope that is also named next. The obvious solution is to rename the local variable, however, I'm fairly new to Python so I'm curious to learn how to prefix the next function so I achieve the desired behavior.

The code I'm using looks something like this:

for prev, curr, next in neighborhood(list):
    if (prev == desired_value):
        print(prev+" "+next)
        desired_value = next(value_iterator)

Note that I'm using Python 3.2.

like image 442
astay13 Avatar asked Jul 12 '12 21:07

astay13


1 Answers

You can use __builtins__.next to refer to the next built-in function.

for prev, curr, next in neighborhood(list):
    if (prev == desired_value):
        print(prev+" "+next)
        desired_value = __builtins__.next(value_iterator)

However, as you point out, the obvious solution is to use a different name for your variable.

like image 91
Mark Byers Avatar answered Nov 15 '22 11:11

Mark Byers