pythonic way to optimize access to imported value?

David Eppstein eppstein at ics.uci.edu
Wed Nov 13 12:32:47 EST 2002


In article <aqu1hd$tjk$0 at 216.39.172.122>, bokr at oz.net (Bengt Richter) 
wrote:

> >	>>> import math
> >	>>> def foo(): return foo.pi
> >	... 
> >	>>> foo.pi = math.pi
> >	>>> foo()
> >	3.1415926535897931
> >
> >I realize this isn't exactly what you are asking for,
> >but it is a way that works today.
> >
> Thanks. It does answer part of the requirement. I.e., you can release the math
> module with del math, but ... it's dependent on a persistent global name
> binding to foo.

How about:

>>> import math
>>> def foo(pi = math.pi): return pi
>>> bar = foo
>>> del math
>>> del foo
>>> bar()
3.1415926535897931

-- 
David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/



More information about the Python-list mailing list