<[EMAIL PROTECTED]> wrote: > 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 -- http://mail.python.org/mailman/listinfo/python-list