[Tutor] Question on "import foobar" vs "from foobar import *"
warpcat at gmail.com
Sun Jan 10 01:23:26 CET 2010
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.
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. ;)
On Sat, Jan 9, 2010 at 8:35 AM, Lowell Tackett <lowelltackett at yahoo.com>wrote:
> If I might offer one small comment...
> It seems to me that this argument also goes to a code readability issue;
> ergo, if you choose "from foobar..." as opposed to "import foobar", then
> from that point on you only need employ foobar's methods in your code, and
> they are not alway easily recognizable. i.e, by importing foobar, you will
> always prefix a method with its' parent module name, which [I think] makes
> the code more understandable.
> From the virtual desk of Lowell Tackett
> --- On *Sat, 1/9/10, Alan Gauld <alan.gauld at btinternet.com>* wrote:
> From: Alan Gauld <alan.gauld at btinternet.com>
> Subject: Re: [Tutor] Question on "import foobar" vs "from foobar import *"
> To: tutor at python.org
> Date: Saturday, January 9, 2010, 10:21 AM
> "Rob Cherry" <pythontutor at lxrb.com<http://email@example.com>>
> > Extending on this advice somewhat - is it *ever* correct to "import
> Yes, it is *usually* correct to "import foobar" and rarely correct to "from
> The exception being if you only need one or two names from foobar, but
> usually you need a lot more, in which case use import foobar.
> To save typing you can give foobar a shorter name:
> import foobar as fb
> For some reason that convenience feature doesn't get mentioned
> all that often but I use it a lot.
> -- Alan Gauld
> Author of the Learn to Program web site
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor