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


Groups > comp.lang.python > #19643

Re: importing module versus using function?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:module': 0.04; 'subject:using': 0.04; 'python:': 0.05; 'typed': 0.07; 'defined.': 0.09; "module's": 0.09; 'namespace': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'underlying': 0.09; 'def': 0.13; '"import': 0.16; 'enigma': 0.16; 'kern': 0.16; 'numpy': 0.16; 'subject:function': 0.16; 'subject:versus': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'header:In-Reply-To:1': 0.22; 'statement': 0.23; 'works.': 0.23; 'code': 0.25; 'compiled': 0.25; 'saying': 0.25; 'module': 0.26; 'pm,': 0.26; 'function': 0.27; 'import': 0.27; 'variable': 0.27; 'work.': 0.28; 'example': 0.28; 'interpret': 0.28; 'confused': 0.30; 'defined,': 0.30; 'imported': 0.30; 'error': 0.30; 'subject:?': 0.30; 'quite': 0.31; 'does': 0.32; 'header:User-Agent:1': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.34; 'header:X-Complaints-To:1': 0.34; 'sequence': 0.36; 'but': 0.37; 'received:org': 0.37; 'getting': 0.37; 'should': 0.38; 'goes': 0.39; 'called': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'world': 0.62; 'received:86': 0.63; 'our': 0.64; 'believe': 0.65; 'therefore,': 0.68; 'skip:y 20': 0.80; 'eco': 0.91; 'from.': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Robert Kern <robert.kern@gmail.com>
Subject Re: importing module versus using function?
Date Tue, 31 Jan 2012 15:53:03 +0000
References <75df179b-e758-4ee7-a37a-744df9459b5c@f14g2000yqe.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host86-181-126-85.range86-181.btcentralplus.com
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:9.0) Gecko/20111222 Thunderbird/9.0.1
In-Reply-To <75df179b-e758-4ee7-a37a-744df9459b5c@f14g2000yqe.googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5248.1328025198.27778.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328025198 news.xs4all.nl 6954 [2001:888:2000:d::a6]:54186
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19643

Show key headers only | View raw


On 1/31/12 3:08 PM, gujax wrote:
> Hi,
> I am confused on this quite bad!!
> If I have this typed in interactive python:
>
>>> import numpy
>
>>> def dummy():
>     y=numpy.arange(1,2,0.1)
>     return y
>
> and then
>>> s = dummy()
>>> s
>>> array[1. , 1.1,  1.2........]
>
>   it works.
>
> But if I have a module called example.py, whose code is
>
> def dummy():
>     y=numpy.arange(1,2,0.1)
>     return y
>
> and now if I do the following:
>
>>> import numpy
>> >from example import *
>>> s=dummy()
>
> The above sequence does not work. It gives an error saying global
> variable numpy not defined.
>
> I understand that when I import a module it gets imported after
> getting compiled and therefore, the module should
> have a statement "import numpy" at its start.
>
> But what if I just want to use the function 'dummy'?

You need to import numpy in dummy.py. When a function needs to look up a name, 
it goes to the module's namespace in which the function is defined, not one of 
the many namespaces where the function is called from.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

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


Thread

importing module versus using function? gujax <rjngrj2010@gmail.com> - 2012-01-31 07:08 -0800
  Re: importing module versus using function? Robert Kern <robert.kern@gmail.com> - 2012-01-31 15:53 +0000

csiph-web