Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #26751
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <gandalf@shopzeus.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'attribute': 0.05; 'sys': 0.05; "'a'": 0.07; 'executed': 0.07; 'python': 0.09; 'imported.': 0.09; 'mkdir': 0.09; 'references,': 0.09; 'thereof.': 0.09; 'file,': 0.15; '2.7.3': 0.16; '__init__.py': 0.16; 'confusion': 0.16; 'finney': 0.16; 'm.a': 0.16; 'modules,': 0.16; 'simpson': 0.16; 'subject:import': 0.16; 'thought.': 0.16; 'variations': 0.16; 'wrote:': 0.17; 'saying': 0.18; '>>>': 0.18; 'module': 0.19; 'code.': 0.20; 'file.': 0.20; 'import': 0.21; '"",': 0.22; 'object.': 0.22; 'example': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(most': 0.27; 'module.': 0.27; 'once,': 0.29; 'writes:': 0.29; 'source': 0.29; 'function': 0.30; 'expect': 0.31; 'code': 0.31; 'gets': 0.32; 'file': 0.32; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'ben': 0.35; 'described': 0.35; 'exist': 0.35; 'false': 0.35; 'same.': 0.35; "won't": 0.35; 'created': 0.36; 'but': 0.36; 'test': 0.36; 'execute': 0.37; 'level': 0.37; 'two': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'first': 0.61; 'different': 0.63; 'subject:...': 0.63; 'more': 0.63; 'touch': 0.69; 'received:204': 0.72; 'object:': 0.84; 'subject:thought': 0.84 |
| Date | Wed, 08 Aug 2012 08:40:46 +0200 |
| From | Laszlo Nagy <gandalf@shopzeus.com> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: I thought I understood how import worked... |
| References | <50211dba$0$29978$c3e8da3$5496439d@news.astraweb.com> <mailman.3071.1344380066.4697.python-list@python.org> <87hasehvfu.fsf@benfinney.id.au> |
| In-Reply-To | <87hasehvfu.fsf@benfinney.id.au> |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| 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.3076.1344408052.4697.python-list@python.org> (permalink) |
| Lines | 65 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1344408052 news.xs4all.nl 6964 [2001:888:2000:d::a6]:60954 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:26751 |
Show key headers only | View raw
On 2012-08-08 06:14, Ben Finney wrote:
> Cameron Simpson <cs@zip.com.au> writes:
>
>> All of you are saying "two names for the same module", and variations
>> thereof. And that is why the doco confuses.
>>
>> I would expect less confusion if the above example were described as
>> _two_ modules, with the same source code.
> That's not true though, is it? It's the same module object with two
> different references, I thought.
They are not the same. Proof:
$ mkdir test
$ cd test
$ touch __init__.py
$ touch m.py
$ cd ..
$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('test')
>>> import m
>>> from test import m
>>> import m
>>> from test import m as m2
>>> m is m2
False
>>> m.a = 3
>>> m2.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'a'
So it is still true that top level code gets executed only once, when
the module is first imported. The trick is that a module is not a file.
It is a module object that is created from a file, with a name. If you
change the name, then you create ("import") a new module.
You can also use the reload() function to execute module level code
again, but it won't create a new module object. It will just update the
contents of the very same module object:
What is more interesting is how the reload() function works:
Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test.m
>>> a = test.m
>>> import os
>>> test.m is a
True
>>> os.system("echo \"import sys\" >> test/m.py")
0
>>> reload(test.m) # Updates the module object
<module 'test.m' from 'test/m.py'>
>>> test.m is a # They are still the same
True
>>> a.sys # So a.sys is a exist
<module 'sys' (built-in)>
>>>
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
I thought I understood how import worked... Roy Smith <roy@panix.com> - 2012-08-07 09:18 -0400
Re: I thought I understood how import worked... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-07 13:52 +0000
Re: I thought I understood how import worked... Roy Smith <roy@panix.com> - 2012-08-07 08:25 -0700
Re: I thought I understood how import worked... Paul Rubin <no.email@nospam.invalid> - 2012-08-07 08:53 -0700
Re: I thought I understood how import worked... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-07 17:54 +0000
Re: I thought I understood how import worked... Cameron Simpson <cs@zip.com.au> - 2012-08-08 08:47 +1000
Re: I thought I understood how import worked... Roy Smith <roy@panix.com> - 2012-08-07 19:05 -0400
Re: I thought I understood how import worked... Ben Finney <ben+python@benfinney.id.au> - 2012-08-08 14:14 +1000
Re: I thought I understood how import worked... Laszlo Nagy <gandalf@shopzeus.com> - 2012-08-08 08:40 +0200
Re: I thought I understood how import worked... Roy Smith <roy@panix.com> - 2012-08-08 09:12 -0400
Re: I thought I understood how import worked... Cameron Simpson <cs@zip.com.au> - 2012-08-09 15:52 +1000
Re: I thought I understood how import worked... Ben Finney <ben+python@benfinney.id.au> - 2012-08-07 23:55 +1000
Re: I thought I understood how import worked... Laszlo Nagy <gandalf@shopzeus.com> - 2012-08-07 16:14 +0200
Re: I thought I understood how import worked... Roy Smith <roy@panix.com> - 2012-08-07 08:32 -0700
Re: I thought I understood how import worked... Terry Reedy <tjreedy@udel.edu> - 2012-08-07 13:15 -0400
Re: I thought I understood how import worked... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-07 15:10 +0100
Re: I thought I understood how import worked... Terry Reedy <tjreedy@udel.edu> - 2012-08-07 12:49 -0400
Re: I thought I understood how import worked... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-07 18:44 +0100
Re: I thought I understood how import worked... Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-08-08 10:47 +0200
csiph-web