pythonic way to optimize access to imported value?

Greg Ewing see_reply_address at something.invalid
Wed Nov 13 19:09:04 EST 2002


Bengt Richter wrote:

> Plus that version retains a reference to math, where I'd like to think that
> I would wind up with just a reference to a double with the 3.14.. value. I.e.,
>      def foo(pi = math.pi):
>          return pi


Yes, that's probably what I should have written.

> Also I think I would like a keyword that governs
> a block scope instead of just the scope of an assignment. E.g.,
> 
>     def foo():
>         predef:
>             import math
>             pi = math.pi
>             del math
>             seven = 7
>         return pi, seven


Too complicated. You could get the same effect by putting the
code outside the def:

    from math import math_pi

    def foo():
       const pi = math_pi
       const seven = 7
       ...

    del math_pi

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg




More information about the Python-list mailing list