Question about name scope

Chris Rebert clp2 at rebertia.com
Wed Feb 1 12:38:59 EST 2012


On Wed, Feb 1, 2012 at 9:11 AM, Olive <diolu at bigfoot.com> wrote:
> I am learning python and maybe this is obvious but I have not been able
> to see a solution. What I would like to do is to be able to execute a
> function within the namespace I would have obtained with  from <module>
> import *
>
> For example if I write:
>
> def f(a):
>        return sin(a)+cos(a)
>
> I could then do:
>
> from math import *
>
> f(5)
>
> But I have polluted my global namespace with all what's defined in
> math. I would like to be able to do something like "from math import *"
> at the f level alone.
>
> The main reason for this is the sympy module for CAS (computer algebra).
> It reimplement a lot of functions and define all the letters as symbolic
> variables. Writing sympy.<function> everywhere is inconvenient.
> Importing all the symbols in the global namespace would lead to name
> clash. It would be nice if I could import all the sympy names but for a
> given function only.

Don't think that's possible. Best alternative I can think of would be:

import sympy as s
def f(a):
    return s.sin(a) + s.cos(a)

Cheers,
Chris



More information about the Python-list mailing list