Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Explicit or general importing of namespaces?

Reply
Thread Tools

Explicit or general importing of namespaces?

 
 
Harlin Seritt
Guest
Posts: n/a
 
      02-28-2005
Is there really a major performance difference between doing the
following:

import Tkinter as TK

TK.Label(yada yada)

OR

from Tkinter import *

Label(yada yada)

I'm unable to tell a real difference other than in the code writing
.

Thanks,

Harlin

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-28-2005
> Is there really a major performance difference between doing the

No.

> I'm unable to tell a real difference other than in the code writing
> .


The difference is that the from-syntax may cause name space pollution. See
the FAQ:

http://www.python.org/doc/faq/programming.html#id12

--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
 
 
 
Michael Hoffman
Guest
Posts: n/a
 
      02-28-2005
Diez B. Roggisch wrote:
>[Harlin Seritt]:
>>Is there really a major performance difference between doing the

>
> No.


Actually, if you are using a name in another module repeatedly
in an inner loop, it can make a big difference if you have to lookup
the name in the module repeatedly. In that case, just explicitly
import the name you want to use repeatedly:

from Tkinter import Label, otherfunction, othervariable

Of course, you are not likely to see this use case when you're doing
UI programming.

But don't do "from Tkinter import *" for the reasons Diez has
identified in the FAQ.
--
Michael Hoffman
 
Reply With Quote
 
Max M
Guest
Posts: n/a
 
      02-28-2005
Michael Hoffman wrote:

> But don't do "from Tkinter import *" for the reasons Diez has
> identified in the FAQ.


It is so annoying to read some code and having to guess from where the
do_complicated_stuff() function is imported from.

Explicit importing is by far the moste preferable.

--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-28-2005
> Actually, if you are using a name in another module repeatedly
> in an inner loop, it can make a big difference if you have to lookup
> the name in the module repeatedly. In that case, just explicitly
> import the name you want to use repeatedly:


Ok - but if that really hits the wall for some part of the code,
then you'd most probably even go for a local variable binding to avoid the
lookups in the different scopes - thus the issue of import format gets
unimportant again

--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      02-28-2005
Diez B. Roggisch wrote:
>>I'm unable to tell a real difference other than in the code writing
>>.

>
> The difference is that the from-syntax may cause name space pollution. See
> the FAQ:
>
> http://www.python.org/doc/faq/programming.html#id12


Ultimately more important than mere "pollution" are the
latent problems this can cause if any of the names in
the original module can ever be re-bound.

Use of the "import *" technique creates module-global names
with bindings to the original objects referenced by the
names in the imported module. If those names (in the imported
module) are rebound (reassigned) at any time by other
code, whether in the module or elsewhere, this has no
effect on the module-global names created in the _importing_
module, which is quite often a bug.

Small demo:

# module A
x = 5

# module B
import A
A.x = 6

# module C
from A import x
import B
print x


This will not print 5, of course. In non-trivial code this
sort of problem is much harder to discover and debug.

In general, unless the names being imported are *guaranteed*
never to be rebound, it is a very bad idea to use "import *",
and it's still a bad idea in almost all cases anyway, for
reasons already given by others.

-Peter
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      02-28-2005
Peter Hansen <(E-Mail Removed)> writes:
> Ultimately more important than mere "pollution" are the
> latent problems this can cause if any of the names in
> the original module can ever be re-bound.


You know, this is another reason the compiler really ought to (at
least optionally) check for such shadowing and have a setting to
enforce user declarations, like perl's "use strict".
 
Reply With Quote
 
Nick Craig-Wood
Guest
Posts: n/a
 
      03-01-2005
Paul Rubin <http> wrote:
> Peter Hansen <(E-Mail Removed)> writes:
> > Ultimately more important than mere "pollution" are the
> > latent problems this can cause if any of the names in
> > the original module can ever be re-bound.

>
> You know, this is another reason the compiler really ought to (at
> least optionally) check for such shadowing and have a setting to
> enforce user declarations, like perl's "use strict".


As a (mostly) ex-perl user I wouldn't like to see python go there - it
seems like a rather slippery slope (like the warnings subsystem).

This is surely a job for pychecker / pylint?

--
Nick Craig-Wood <(E-Mail Removed)> -- http://www.craig-wood.com/nick
 
Reply With Quote
 
Peter Mayne
Guest
Posts: n/a
 
      03-01-2005
Peter Hansen wrote:
>
> In general, unless the names being imported are *guaranteed*
> never to be rebound, it is a very bad idea to use "import *",
> and it's still a bad idea in almost all cases anyway, for
> reasons already given by others.


Since I've been playing with PyQt lately...

Is qt not one of the "almost all" cases? From the limited number of
examples I've seen, it seems to be common to do

from qt import *

Since most of the imported names start with "Q", are called QLabel,
QSlider, etc, and are generally recognisable in context, this would seem
to be a reasonable case of namespace pollution.

I'm certainly not arguing with the general premise, just wondering if qt
is one of the sensible exceptions.

PJDM
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      03-04-2005
Peter Mayne wrote:
> Peter Hansen wrote:
>> and it's still a bad idea in almost all cases anyway

>
> Since I've been playing with PyQt lately...
>
> Is qt not one of the "almost all" cases? From the limited number of
> examples I've seen, it seems to be common to do
>
> from qt import *


This sort of thing seems common amongst large frameworks such
as PyQt or wxPython. This is unfortunate, IMHO, though it isn't
really a serious concern for most users.

I'm grateful that the most recent versions of wxPython have
abandoned that approach in favour of a nice clean "import wx",
and as far as I can tell the code does not suffer as a result,
and gains substantially in clarity. Maybe the "qt" module
defines far fewer names than the "wx" module does, but I for
one am glad not to have to worry that I won't accidentally
conflict with the hundreds that are there (in wx), nor to
worry that my code lacks in readability.

> Since most of the imported names start with "Q", are called QLabel,
> QSlider, etc, and are generally recognisable in context, this would seem
> to be a reasonable case of namespace pollution.
>
> I'm certainly not arguing with the general premise, just wondering if qt
> is one of the sensible exceptions.


If not sensible, at least fairly widely accepted, not a serious
impediment to effective use, and definitely not without precedent.

-Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
General....very general.... no important for u forever hi Python 0 03-18-2009 08:21 AM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
How to keep a module with the same name as a module it is importing from importing itself? plb Python 2 02-08-2005 03:14 PM
What's the difference betwwen explicit instantiaion and explicit specialization? Andy C++ 5 01-30-2005 11:46 PM
Is explicit template qualification required for explicit delete? J.T. Conklin C++ 1 08-11-2004 02:06 AM



Advertisments