[Tutor] Question on "import foobar" vs "from foobar import *"

Lie Ryan lie.1296 at gmail.com
Sun Jan 10 06:08:40 CET 2010


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.



More information about the Tutor mailing list