Explicit or general importing of namespaces?

H

Harlin Seritt

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
 
M

Michael Hoffman

Diez said:
>[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.
 
M

Max M

Michael said:
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
 
D

Diez B. Roggisch

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 :)
 
P

Peter Hansen

Diez said:
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
 
P

Paul Rubin

Peter Hansen said:
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".
 
N

Nick Craig-Wood

Paul Rubin said:
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?
 
P

Peter Mayne

Peter said:
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
 
P

Peter Hansen

Peter said:
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
 
H

Harlin Seritt

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top