Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

newbie: from .. import *

0 views
Skip to first unread message

Jean-Sébastien Bolduc

unread,
Jun 13, 2002, 6:37:38 PM6/13/02
to
Hi All

I've been working with Python for a while, but never really felt
confortable with the way namespaces are imported.

I'm working on a simple application that is to be used in the
interactive mode. The idea is that the behavior of a function is
determined, in part, by some global(?) flags. So I can change my
flags, and explicitely call my function. Really simple. Except that
for some reason, it does not work.

In "test.py", I have the following:

flag = 1
def foo():
print flag
foo()
flag = 0
foo()

In the interactive mode, if I import the code above as:
>>> from test import *
1
0

The output is as expected. However, if I continue:
>>> flag = 1
>>> foo()
0

(!) Okay... not what _I_ expected. It sounds really silly, but I've
spent a couple of hours on that, without success. I thought at some
point that using "global" would help me. Of course, if I just do
"import test", everything works fine --- but in interactive mode, I
don't want to type "test.foo()"

Some help?
--JSeb

Chris Liechti

unread,
Jun 13, 2002, 6:54:37 PM6/13/02
to
js...@cs.mcgill.ca (Jean-Sébastien Bolduc) wrote in
news:56e1eff0.02061...@posting.google.com:

the function still runs in the namespace of its module.
you can explicit use a module to store global values. e.g. you could
use __main__.

test.py:
import __main__
__main__.flag = 0
def foo():
print __main__.flag
...

of course you can use any module for that, not only __main__.

chris

--
Chris <clie...@gmx.net>

James Rowe

unread,
Jun 13, 2002, 6:40:52 PM6/13/02
to
On 13 Jun 2002 15:37:38 -0700, Jean-Sébastien Bolduc <js...@cs.mcgill.ca> wrote:
>Hi All
>
>I've been working with Python for a while, but never really felt
>confortable with the way namespaces are imported.
>
>I'm working on a simple application that is to be used in the
>interactive mode. The idea is that the behavior of a function is
>determined, in part, by some global(?) flags. So I can change my
>flags, and explicitely call my function. Really simple. Except that
>for some reason, it does not work.
>
>In "test.py", I have the following:
>
> flag = 1
> def foo():
> print flag
> foo()
> flag = 0
> foo()
>
>In the interactive mode, if I import the code above as:
>>>> from test import *
>1
>0
>
>The output is as expected. However, if I continue:
>>>> flag = 1
>>>> foo()
>0
>

# bar.py

flag = 1
def foo():
print flag
foo()

flag = 2
foo()


>>> from bar import *
1
2
>>> import bar
>>> foo()
2
>>> flag = 3
>>> foo()
2
>>> bar.flag = 4
>>> foo()
4


Aahz

unread,
Jun 14, 2002, 2:31:56 AM6/14/02
to
In article <56e1eff0.02061...@posting.google.com>,

=?ISO-8859-1?Q?Jean-S=E9bastien_Bolduc?= <js...@cs.mcgill.ca> wrote:
>
>I've been working with Python for a while, but never really felt
>confortable with the way namespaces are imported.

What you're dealing with isn't, strictly speaking, a namespace issue but
a scope issue. Python uses lexical scope to determine visibility of
module globals and locals, so that static analysis of source can
determine visibility of names. Therefore, when you define a function,
its scope stays with the module where you defined the function.
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

"I had lots of reasonable theories about children myself, until I
had some." --Michael Rios

0 new messages