Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18769 > unrolled thread
| Started by | Rod Kosloski <rkosloski@visi.com> |
|---|---|
| First post | 2012-01-10 10:01 -0600 |
| Last post | 2012-01-10 17:40 +0100 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
Perplexed by the behavior of import Rod Kosloski <rkosloski@visi.com> - 2012-01-10 10:01 -0600
Re: Perplexed by the behavior of import Peter Otten <__peter__@web.de> - 2012-01-10 17:40 +0100
| From | Rod Kosloski <rkosloski@visi.com> |
|---|---|
| Date | 2012-01-10 10:01 -0600 |
| Subject | Perplexed by the behavior of import |
| Message-ID | <m2sjjno8ba.fsf@news.visi.com> |
I'm perlexed by an apparent inconsistency in the behavior of the import
statement.
First, the files. There is a simple package, pkg, containing two files: mod.py
and util.py, and a stand-alone module also named util.py:
*** ./pkg/__init__.py ***
from mod import *
*** ./pkg/mod.py ***
M = 8
*** ./pkg/util.py ***
V = 0
*** ./util.py ***
from pkg import *
from pkg.util import *
U = 0
Next, the Python session:
Python 2.6.4 (r264:75706, Dec 13 2009, 19:46:11)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import util
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__',
'__doc__': None 'util': <module 'util' from 'util.pyc'>, '__package__':
None}
>>> from pkg import *
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, 'M': 8, '__package__':
None, 'util': <module 'pkg.util' from 'pkg/util.pyc'>, '__name__':
'__main__', '__doc__': None,
'mod': <module 'pkg.mod' from 'pkg/mod.pyc'>}
Compare the output of the two globals() statements:
Variable util's value has changed from <module 'util' from 'util.py'....> to
<module 'pkg.util'...>
What's happening to util?
OK, maybe pkg.util replaces the original util because of the pkg import
statement. But then, what about the following:
Python 2.6.4 (r264:75706, Dec 13 2009, 19:46:11)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> util = 0
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__',
'__doc__': None, 'util': 0, '__package__': None}
>>> from pkg import *
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, 'M': 8, '__package__':
None, 'util': 0, '__name__': '__main__', '__doc__': None, 'mod': <module
'pkg.mod' from 'pkg/mod.pyc'>}
>>>
Now the value of util is unchanged across the pkg import statement.
Why the difference?
Thanks.
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-01-10 17:40 +0100 |
| Message-ID | <mailman.4605.1326213611.27778.python-list@python.org> |
| In reply to | #18769 |
Rod Kosloski wrote:
> I'm perlexed by an apparent inconsistency in the behavior of the import
> statement.
>
> First, the files. There is a simple package, pkg, containing two files:
> mod.py and util.py, and a stand-alone module also named util.py:
>
> *** ./pkg/__init__.py ***
> from mod import *
> *** ./pkg/mod.py ***
> M = 8
> *** ./pkg/util.py ***
> V = 0
> *** ./util.py ***
> from pkg import *
> from pkg.util import *
> U = 0
>
> Next, the Python session:
>
> Python 2.6.4 (r264:75706, Dec 13 2009, 19:46:11)
> [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import util
> >>> globals()
> {'__builtins__': <module '__builtin__' (built-in)>, '__name__':
> {'__main__',
> '__doc__': None 'util': <module 'util' from 'util.pyc'>,
> '__package__': None}
> >>> from pkg import *
> >>> globals()
> {'__builtins__': <module '__builtin__' (built-in)>, 'M': 8,
> {'__package__':
> None, 'util': <module 'pkg.util' from 'pkg/util.pyc'>, '__name__':
> '__main__', '__doc__': None,
> 'mod': <module 'pkg.mod' from 'pkg/mod.pyc'>}
>
> Compare the output of the two globals() statements:
> Variable util's value has changed from <module 'util' from 'util.py'....>
> to <module 'pkg.util'...>
>
> What's happening to util?
>
> OK, maybe pkg.util replaces the original util because of the pkg import
> statement.
That's indeed what happens.
> But then, what about the following:
>
> Python 2.6.4 (r264:75706, Dec 13 2009, 19:46:11)
> [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> util = 0
> >>> globals()
> {'__builtins__': <module '__builtin__' (built-in)>, '__name__':
> {'__main__',
> '__doc__': None, 'util': 0, '__package__': None}
> >>> from pkg import *
> >>> globals()
> {'__builtins__': <module '__builtin__' (built-in)>, 'M': 8,
> {'__package__':
> None, 'util': 0, '__name__': '__main__', '__doc__': None, 'mod':
> <module 'pkg.mod' from 'pkg/mod.pyc'>}
> >>>
>
> Now the value of util is unchanged across the pkg import statement.
>
> Why the difference?
As long as you haven't imported the pkg.util module there isn't an "util"
variable in the pkg namespace:
$ python -c 'import pkg; print "util" in vars(pkg); import pkg.util; print
"util" in vars(pkg)'
False
True
Or, sticking closer to your example:
$ python -c 'util = 0; from pkg import *; print util'
0
$ python -c 'util = 0; import pkg.util; from pkg import *; print util'
<module 'pkg.util' from 'pkg/util.pyc'>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web