Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'defines': 0.07; 'function,': 0.07; 'space.': 0.07; 'specifying': 0.07; 'symbols': 0.07; 'python': 0.08; 'namespace': 0.09; 'variables.': 0.09; 'worked,': 0.09; 'exception': 0.12; 'def': 0.13; 'library': 0.13; '2.7': 0.13; 'cc:addr:python-list': 0.15; '"from': 0.16; '"normal"': 0.16; 'received:192.168.1.104': 0.16; 'symbolic': 0.16; 'wrote:': 0.16; 'solution.': 0.17; 'linux': 0.17; 'subject:Question': 0.19; 'cc:no real name:2**0': 0.21; 'file,': 0.21; 'maybe': 0.21; 'header:In-Reply-To:1': 0.22; 'interface': 0.23; 'defined': 0.24; 'math': 0.24; 'cc:2**0': 0.25; 'module': 0.26; 'pm,': 0.26; 'function': 0.27; 'import': 0.27; 'tried': 0.27; "i'm": 0.27; 'needed,': 0.28; 'bit': 0.28; 'example': 0.28; 'explicitly': 0.28; 'importing': 0.28; 'print': 0.29; 'cc:addr:python.org': 0.29; 'writer': 0.30; 'usually': 0.30; 'version': 0.31; 'does': 0.32; 'source': 0.32; 'it.': 0.33; 'header:User-Agent:1': 0.33; 'normally': 0.34; 'function.': 0.34; 'surprised': 0.34; 'something': 0.35; 'things': 0.35; "i'll": 0.35; 'operating': 0.35; 'entry': 0.37; 'but': 0.37; 'using': 0.37; 'could': 0.37; 'put': 0.38; 'received:192': 0.38; 'form,': 0.38; "what's": 0.39; 'define': 0.39; 'received:192.168.1': 0.39; 'subject:: ': 0.39; 'type': 0.60; 'your': 0.61; 'subject:name': 0.67; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'obtained': 0.80; 'experiment': 0.84; 'way)': 0.91; 'alone.': 0.93 Date: Wed, 01 Feb 2012 12:36:36 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 Thunderbird/3.1.16 MIME-Version: 1.0 To: Olive Subject: Re: Question about name scope References: <20120201181117.5d35dddc@bigfoot.com> In-Reply-To: <20120201181117.5d35dddc@bigfoot.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:gIuP2gKgbQTepDd18MvlXUq0c11ZHzP6/yuhohga+cT SNtg8S5GON5b/Yw/Iltjeo+QGHNA+WGJFWiioSzL8DHoOACxgD GBzH6SLVT5mY38ZR8d1HAu3FH1puLvd0t3NxsIbE/Mt5ESNWEp zmTlN+zD4XlOm6zttjoBlN/Mry88Egw0aaAQdz8OsbMAXm3+7p RsLwLywZ98x8umowGQn2ELF2t//7vr1kJvtJmr9XHLUmEMXpFm oErKn7ymH0b0Jaye6KczdgQy0gPv+rZLPpD652xfF2TyqjIY2C koYEU4bX5eL8GtQVaiTvqiJUes1kBb8+/u2Zm7ypPlkmZF63A= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: d@davea.name List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 86 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328117874 news.xs4all.nl 6893 [2001:888:2000:d::a6]:53836 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19727 On 02/01/2012 12:11 PM, 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 > 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. 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. > > Olive > > Start by specifying python version and operating system. I tried your experiment using Python 2.7 and Linux 11.04 def f(a): from math import sin, cos return sin(a) + cos(a) print f(45) Does what you needed, and neatly. The only name added to the global namspace is f, of type function. I was a bit surprised that using from math import * inside the function worked, but it generates a warning: olive.py:2: SyntaxWarning: import * only allowed at module level def f(a): I normally avoid any use of the "from XX import *" form, as it pollutes the global name space. The only exception is when a library writer documents that this is the "normal" way to interface to it. In this case, he usually defines just a few things that are visible this way) What I do is put a single import math at the top of my source file, and use math.sin, and math.cos where needed. Occasionally, I'll use something like: from math import sin,cos at the top, so I know just which symbols I'm defining. How about: import math def f(a): sin = math.sin cos = math.cos return sin(a) + cos(a) print f(45) This lets you explicitly use the sin and cos names inside the function, by defining them at entry to the function. -- DaveA