timeit's environment

R

rurpy

Why doesn't the following work? It generates a "NameError: global
name 'data' is not defined" error.

import timeit

global data
data = [3,8,4,8,6,0,5,7,2,1]

env = "global data; x = data"

print timeit.Timer('x.sort()', env).timeit()
print timeit.Timer('x.sort(cmp=cmp', env).timeit()

How can I get timeit() to see an external (to it) variable?
(In the real program 'data' is very expensive to create and
contains non-reproducable data and the two timeit calls
must be run on identical objects.
 
A

Alex Martelli

Why doesn't the following work? It generates a "NameError: global
name 'data' is not defined" error.

import timeit

global data
data = [3,8,4,8,6,0,5,7,2,1]

env = "global data; x = data"

print timeit.Timer('x.sort()', env).timeit()
print timeit.Timer('x.sort(cmp=cmp', env).timeit()

How can I get timeit() to see an external (to it) variable?
(In the real program 'data' is very expensive to create and
contains non-reproducable data and the two timeit calls
must be run on identical objects.

You have to use 'from __main__ import data as x' rather than just say
'global data; x=data', because timeit is a separate module from your
__main__ one.


Alex
 
R

rurpy

Alex said:
Why doesn't the following work? It generates a "NameError: global
name 'data' is not defined" error.

import timeit

global data
data = [3,8,4,8,6,0,5,7,2,1]

env = "global data; x = data"

print timeit.Timer('x.sort()', env).timeit()
print timeit.Timer('x.sort(cmp=cmp', env).timeit()

How can I get timeit() to see an external (to it) variable?
(In the real program 'data' is very expensive to create and
contains non-reproducable data and the two timeit calls
must be run on identical objects.

You have to use 'from __main__ import data as x' rather than just say
'global data; x=data', because timeit is a separate module from your
__main__ one.

Ahh, (slaps forehead) that makes sense. Thank you.

After posting I looked again at the documentation and at the
bottom of the example subsection, they also mention using
import (although without explaining why.)

Since I've been bitching about documentation in another
thread, I'm curious... Would it be obvious to anyone of
low to intermediate python skills that using global would
not work in this case? Would it be obvious that using an
import is the answer? Or can I blame this partially on the
documentation? :) I think the scoping issue could have
at least been mentioned in the Timer class or timeit method
descriptions. There is no mention there of exactly what
environment the code is run in.
 
S

Scott David Daniels

Since I've been bitching about documentation in another
thread, I'm curious... Would it be obvious to anyone of
low to intermediate python skills that using global would
not work in this case? Would it be obvious that using an
import is the answer? Or can I blame this partially on the
documentation? :) I think the scoping issue could have
at least been mentioned in the Timer class or timeit method
descriptions. There is no mention there of exactly what
environment the code is run in.

Perhaps you could write a paragraph or two that would have
informed you and send it to the destination mentioned on the
documentation page.
 
R

rurpy

Scott said:
Perhaps you could write a paragraph or two that would have
informed you and send it to the destination mentioned on the
documentation page.

Yes, but I was hoping to get some sense of how such
a submission might be viewed prior to going through
the work of doing it.
 
S

Scott David Daniels

Scott David Daniels wrote: ....

Yes, but I was hoping to get some sense of how such
a submission might be viewed prior to going through
the work of doing it.
I assure you that if it does seem to ease the pain we'd love to
include it. A newbie knows what's confusing about the intro parts.
Once you've been around a while, you no longer even read those
intro things, and so don't know where they suffer. "It's to hard
for me to understand" is a content-free complaint that is too often
heard, but "if you said, 'In this instance be sure to ...' in paragraph
three ..." is a clear explanation of what is wrong and how it might be
fixed that will definitely be read.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top