Question about name scope
Chris Kaynor
ckaynor at zindagigames.com
Wed Feb 1 18:28:06 CET 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.
>
The standard way would be to just do:
import math
def f(a):
return math.sin(a)+math.cos(a)
What this does is import the module math into the current module namespace,
then access attributes on that module.
>
> Olive
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120201/441407eb/attachment.html>
More information about the Python-list
mailing list