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

Eric Pavey warpcat at gmail.com
Sun Jan 10 22:11:59 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.
>

See, I figured I'd get straightened out myself by posting that.  Those
concepts help me out as well ;)  Always learning...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100110/0439736c/attachment.htm>


More information about the Tutor mailing list