Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18772
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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; 'example:': 0.03; 'package,': 0.03; 'next,': 0.05; '2.6.4': 0.07; 'rod': 0.07; 'python': 0.08; 'files:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'statement.': 0.09; 'files.': 0.09; 'output': 0.10; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; '[gcc': 0.16; 'happens.': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'replaces': 0.16; 'sticking': 0.16; 'subject:behavior': 0.16; 'subject:import': 0.16; 'wrote:': 0.18; '>>>': 0.18; "haven't": 0.20; 'maybe': 0.21; 'dec': 0.22; 'changed': 0.23; 'from:addr:web.de': 0.23; 'happening': 0.24; 'module': 0.26; "i'm": 0.26; 'import': 0.27; 'variable': 0.28; 'compare': 0.28; "skip:' 10": 0.29; 'print': 0.29; 'closer': 0.30; 'imported': 0.30; 'none,': 0.30; 'ok,': 0.31; "isn't": 0.33; 'header:X -Complaints-To:1': 0.33; 'named': 0.33; 'there': 0.33; 'to:addr :python-list': 0.34; 'none': 0.37; 'two': 0.37; 'but': 0.37; 'received:org': 0.38; 'why': 0.39; 'or,': 0.40; 'to:addr:python.org': 0.40; 'more': 0.61; 'type': 0.61; 'your': 0.61; '***': 0.73; '4.2.1': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Perplexed by the behavior of import |
| Date | Tue, 10 Jan 2012 17:40:04 +0100 |
| Organization | None |
| References | <m2sjjno8ba.fsf@news.visi.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p5084b977.dip.t-dialin.net |
| 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.4605.1326213611.27778.python-list@python.org> (permalink) |
| Lines | 87 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1326213611 news.xs4all.nl 6847 [2001:888:2000:d::a6]:37474 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:18772 |
Show key headers only | View raw
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'>
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
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
csiph-web