Using a dict as if it were a module namespace.

Hendrik van Rooyen mail at
Mon Jan 28 12:52:01 CET 2008

 On Sunday 27 January 2008 09:45, Steven D'Aprano wrote:
 > I have a problem which I think could be solved by using a dict as a
 > namespace, in a similar way that exec and eval do.
 > When using the timeit module, it is very inconvenient to have to define
 > functions as strings. A good alternative is to create the function as
 > normal, and import it:
 > def myfunc(x, y):
 >     return x+y
 > timeit.Timer("myfunc(59, 60)", "from __main__ import myfunc").timeit()
 > Not only is this an easy idiom to follow, but myfunc can live in another
 > module: just replace __main__ with the module name.
 > Now, I'm trying to build a suite of tests to use with timeit. I have a
 > bunch of tests which I've created as dicts:
 > test_suite= [dict(x=59, y=60), dict(x=-1, y=-2)]
 This is probably the wrong answer:
 test_suite = [(59,60),(-1,-2)]
 for test in test_suite:
     x,y = test
 Then do the magic with x and y
 - Hendrik

More information about the Python-list mailing list