Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19726

Re: Question about name scope

Date 2012-02-01 09:43 -0800
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: Question about name scope
References <20120201181117.5d35dddc@bigfoot.com>
Newsgroups comp.lang.python
Message-ID <mailman.5310.1328117843.27778.python-list@python.org> (permalink)

Show all headers | View raw


Olive 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.

If you are using Python 2.x you can do:

     def f(a):
         from sympy import *
         return a(a) + d(a)

Python 3 does not allow * imports in functions, however, so you would 
need to do:

     def f(a):
         from sympy import a,b,c,d,e,f,g,h,i,j,k,l,m
         from sympy import n,o,p,q,r,s,t,u,v,w,x,y,z
         return z(a) / f(a) + o(a)

Obviously, only import the functions you are actually going to use.  ;)

~Ethan~

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Question about name scope Olive <diolu@bigfoot.com> - 2012-02-01 18:11 +0100
  Re: Question about name scope Rick Johnson <rantingrickjohnson@gmail.com> - 2012-02-01 09:21 -0800
  Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 09:43 -0800
  Re: Question about name scope Dave Angel <d@davea.name> - 2012-02-01 12:36 -0500
    Re: Question about name scope Mel Wilson <mwilson@the-wire.com> - 2012-02-01 13:47 -0500
      Re: Question about name scope Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-01 14:49 -0700
      Re: Question about name scope Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-01 15:38 -0700
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 14:24 -0800
      Re: Question about name scope Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-01 16:00 -0700
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 15:08 -0800
      Re: Question about name scope Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-01 16:47 -0700
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 14:53 -0800
        Re: Question about name scope Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-02 00:34 +0000
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 15:59 -0800
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 15:41 -0800
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 15:51 -0800
  Re: Question about name scope Chris Rebert <clp2@rebertia.com> - 2012-02-01 09:38 -0800
  Re: Question about name scope Christian Heimes <lists@cheimes.de> - 2012-02-01 18:50 +0100

csiph-web