1
def main():
    range_dur = 0
    xrange_dur = 0

    def do_range():
        start = time.time()
        for i in range(2,10):
            print i
        range_dur += time.time() - start              <---- cant see
    def do_xrange():
        start = time.time()
        for i in xrange(2,10):
            print i
        xrange_dur += time.time() - start             <---- cant see

    do_range()
    do_xrange()
    print range_dur
    print xrange_dur

if __name__ == '__main__':
    main()

I know I could put range_dur and xrange_dur outside of main() and have them called global inside do_range() and do_xrange() to be seen but how come the above doesnt work?

Whats the inner-working of variable visibility for python?

or is there another solution besides using it as global?

1
  • 1
    Alternately you could pass them as parameters to the corresponding function and get computation as return value.
    – Leon Young
    Commented Oct 12, 2013 at 17:18

2 Answers 2

3

If you are using Python 3, see pandubear's answer - nonlocal is the way to go.

If you are using Python 2 you need to use a workaround based on mutable objects so you avoid the rebinding caused by +=. The easiest way to do this is wrapping the value in a list:

def main():
    range_dur = [0]
    xrange_dur = [0]

    def do_range():
        start = time.time()
        for i in range(2,10):
            print i
        range_dur[0] += time.time() - start

    def do_xrange():
        start = time.time()
        for i in xrange(2,10):
            print i
        xrange_dur[0] += time.time() - start             

    do_range()
    do_xrange()
    print range_dur[0]
    print xrange_dur[0]

if __name__ == '__main__':
    main()

Another option - at least for a super-simple benchmark script like yours - would be using global variables. Then you can use global instead of nonlocal. It's not pretty but for a benchmark prettiness does not really matter.

The best solution however is simply using return to return the result!

Oh, and did you know about the timeit module? It does what you are trying to do; just in a nicer way.

2
  • python is purposely designed this way to limit programmers to pass param around? ah awesome timeit module
    – ealeon
    Commented Oct 12, 2013 at 17:23
  • Rather than using a list to wrap the values I like to use a class (named nonlocal so you know it's serving the same purpose as nonlocal in Python 3). Then you can access nonlocal.x etc.
    – kindall
    Commented Oct 12, 2013 at 17:38
2

You can see those variables, -- that is, get their values -- but you can't set their values without using a nonlocal statement. Use this:

def main():
    range_dur = 0
    xrange_dur = 0

    def do_range():
        nonlocal range_dur
        start = time.time()
        for i in range(2,10):
            print i
        range_dur += time.time() - start
    def do_xrange():
        nonlocal xrange_dur
        start = time.time()
        for i in xrange(2,10):
            print i
        xrange_dur += time.time() - start             

    do_range()
    do_xrange()
    print range_dur
    print xrange_dur

if __name__ == '__main__':
    main()
3
  • @ealeon Replace nonlocal with global Commented Oct 12, 2013 at 21:26
  • @SethMMorton only if i put range_dur and xrange_dur outside of main
    – ealeon
    Commented Oct 12, 2013 at 22:52
  • Is there a reason you don't want to pass range_dur and xrange_dur as parameters to the functions, and return values from those functions? Commented Oct 12, 2013 at 22:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.