468,321 Members | 1,799 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,321 developers. It's quick & easy.

Explicit or general importing of namespaces?

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

Jul 18 '05 #1
10 1244
> 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
Jul 18 '05 #2
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
Jul 18 '05 #3
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
Jul 18 '05 #4
> 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
Jul 18 '05 #5
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
Jul 18 '05 #6
Peter Hansen <pe***@engcorp.com> 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".
Jul 18 '05 #7
Paul Rubin <http> wrote:
Peter Hansen <pe***@engcorp.com> 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 <ni**@craig-wood.com> -- http://www.craig-wood.com/nick
Jul 18 '05 #8
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
Jul 18 '05 #9
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
Jul 18 '05 #10
I think the bottom line on this is using your own sense of risk/reward with
each given module imported. Some modules (Tkinter comes to mind) it makes
sense to pollute while others it doesn't.

Harlin

"Peter Hansen" <pe***@engcorp.com> wrote in message
news:4K********************@powergate.ca...
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

Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by qwweeeit | last post: by
1 post views Thread by Steve George | last post: by
29 posts views Thread by Natan | last post: by
5 posts views Thread by Christoffer | last post: by
1 post views Thread by ARC | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.