[Tutor] Tutor Digest, Vol 71, Issue 28

Make Twilight ph4nut at gmail.com
Mon Jan 11 11:41:46 CET 2010


On Sat, Jan 9, 2010 at 9:08 PM, Lie Ryan <lie.1296 at gmail.com> wrote:
> On 1/10/2010 11:23 AM, Eric Pavey wrote:
>
>> I should add (that as I understand it), when you do a 'from foo import
>> blah', or 'from foo import *', this is doing a /copy/ (effectively) of
>> that module's attributes into the current namespace.  Doing "import foo"
>> or "import foo as goo" is keeping a /reference /to the imported module
>> rather than a copy.
>>
>
> No, that's a roundabout way to look at it. Python's variable holds
> references to objects[1] and never the object themselves; name assignment
> statement in python never makes a copy of the object, but always makes a
new
> reference to the same object. "Assignment statements" in python includes
the
> '=', 'from import', and regular 'import' [2].
>
> [1] this is call-by-object http://effbot.org/zone/python-objects.htm
> http://effbot.org/zone/call-by-object.htm
> [2] there are other more obscure statements that is an 'assignment
> statement' as well, such as "with ... as ...", "agumented assignment
> operators", dictionary/list assignment, etc. The list is non-exhaustive.
>
>
>  If you use the 'from import' system, changes made to attrs of the
>> imported module /won't/ be seen by any other module that imported it.
>> If you do just an 'import' on a module (or 'import ... as ...'), then
>> changes made to attrs on the imported module /will /be seen by othe
>> modules that import it as well.  I hope that is somewhat clear. ;)
>>
>
> Read both links to effbot's article, they should make it clear why the
> current behavior is the way it is.
>

I am confuse on the text above:
"If you use the 'from import' system, changes made to attrs of the
 imported module /won't/ be seen by any other module that imported it.
 If you do just an 'import' on a module (or 'import ... as ...'), then
 changes made to attrs on the imported module /will /be seen by othe
 modules that import it as well.  I hope that is somewhat clear. ;)"

I had tried to simulate this situation:
------------ Module a-----------
#!usr/bin/env ptyhon
#Module  a.py
name = "a::name"

------------Module b------------
#usr/bin/env python
#Module  b.py
form a import *

while Ture:
    blah = rawinput("input something:")
    print a.name

------------Module c------------
#usr/bin/env python
#Module c.py
import a

 while Ture:
    blah = rawinput("input something:")
    print a.name

------------end-------------------
when i excuted b.py,c.py and modified  the attr name = "a::newname",but b.py
and c.py were still output "a::name".
what's the problem?
does it the right way python  takes?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100111/a469a36f/attachment.htm>


More information about the Tutor mailing list