[Tutor] Tutor Digest, Vol 71, Issue 28

Alan Gauld alan.gauld at btinternet.com
Mon Jan 11 11:57:51 CET 2010


"Make Twilight" <ph4nut at gmail.com> wrote
> 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:   # True?
>    blah = rawinput("input something:")
>    print a.name

This should not work because you have not imported "a" only "name" from a.
This should therefore say

print name


> ------------Module c------------
> #usr/bin/env python
> #Module c.py
> import a
>
> while Ture:
>    blah = rawinput("input something:")
>    print a.name

But this is OK.

> 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".

How did you execute them? There is nothing to change the attributes
here so how did you change them? If you altered the source code of
a after the imports were run then the changes will not be seen.

> what's the problem?
> does it the right way python  takes?

I'm not sure there is a problem. It depends on how you are conducting
the test and what you expect the answer to be.

Alan G. 




More information about the Tutor mailing list