[Tutor] What's the problem with my SpinSpeeds.py?

Kent Johnson kent_johnson at skillsoft.com
Sun Aug 1 19:05:59 CEST 2004


Dick,

The key to understanding this is to realize that the execution of 
countUsingRange(n) happens in a separate module with its own namespace. 
Here is a simpler example to show what is going on:

Suppose a.py contains this code:
def printSomething(a):
     print 'Something is', a

n = 3

import b
b.test()

and b.py contains this:
def test():
     from __main__ import printSomething
     printSomething(100)

Running a.py results in the printout
Something is 100

The important thing to notice is the 'from __main__ import printSomething' 
in b.py. Without this line, test() would not know what printSomething means 
and it would throw aNameError when it is invoked. (Note: a.py is being run 
as main, so its module name is __main__. If a were run by importing it from 
another module, the import in b would be 'from a import ...')

Now suppose you change b.py to
def test():
     from __main__ import printSomething
     printSomething(n)

When you run this, you will get a NameError because module b has nothing 
named n.

The solution is to change b.py to also import n:
def test():
     from __main__ import printSomething, n
     printSomething(n)

Now it works.

What does this have to do with your question? Well, a.py is analogous to 
your main program. b.py corresponds to the timeit module. timeit is using 
the exec statement to run the code you pass it, but the same scoping rules 
apply.

So the solution is to add n to the import statements you pass to Timer(), 
for example
     t = Timer("countUsingRange(25000)", "from __main__ import 
countUsingRange, n")

Kent

At 09:36 AM 8/1/2004 -0700, Dick Moores wrote:
>Why am I unable to use countUsingRange(n) instead of countUsingRange(25000)?
>
>When I do so, I get
>
>Traceback (most recent call last):
>   File "C:/Python23/1.py", line 17, in -toplevel-
>     rangeTime = t.timeit(repetitions)
>   File "C:\Python23\lib\timeit.py", line 158, in timeit
>     return self.inner(it, self.timer)
>   File "<timeit-src>", line 6, in inner
>NameError: global name 'n' is not defined
>
>Is this fixable?
>
>Dick
>
>=====================================
>#SpinSpeeds.py
>
>def countUsingRange(n):
>     for i in range(n):
>         pass
>
>def countUsingXrange(n):
>     for i in xrange(n):
>         pass
>
>if __name__=='__main__':
>     from timeit import Timer
>     repetitions = 1000
>     n = 25000
>
>     t = Timer("countUsingRange(25000)", "from __main__ import 
> countUsingRange")
>     rangeTime = t.timeit(repetitions)
>
>     t = Timer("countUsingXrange(25000)", "from __main__ import 
> countUsingXrange")
>     xrangeTime = t.timeit(repetitions)
>
>     print " range(%d) time = %f for %d repetitions" % (n, rangeTime, 
> repetitions)
>     print "xrange(%d) time = %f for %d repetitions" % (n, xrangeTime, 
> repetitions)
>==========================================
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list