Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19642 > unrolled thread
| Started by | gujax <rjngrj2010@gmail.com> |
|---|---|
| First post | 2012-01-31 07:08 -0800 |
| Last post | 2012-01-31 15:53 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
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
| From | gujax <rjngrj2010@gmail.com> |
|---|---|
| Date | 2012-01-31 07:08 -0800 |
| Subject | importing module versus using function? |
| Message-ID | <75df179b-e758-4ee7-a37a-744df9459b5c@f14g2000yqe.googlegroups.com> |
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'? I tried the following: from example import dummy and then try to use the function, it still gives me 'global name numpy not defined'! Why is that happening? Any pointers please. Thanks
[toc] | [next] | [standalone]
| From | Robert Kern <robert.kern@gmail.com> |
|---|---|
| Date | 2012-01-31 15:53 +0000 |
| Message-ID | <mailman.5248.1328025198.27778.python-list@python.org> |
| In reply to | #19642 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web