473,796 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1428
> 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2408
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In some cases there is a further complication: module importing through an indirect mechanism, like: exec "from " + xxx + " import *". A part the fact that I have not understood the "real" difference between import and from ... import (or also from......
1
2238
by: Steve George | last post by:
Hi, I have a scenario where I have a master schema that defines a number of complex and simple types. I then have a number of other schemas (with different namespaces) where I would like to reuse some of these master complex and simple types. This I believe will assist me in transforming between the master schema and the other smaller schemas that contain a subset of the elements in the master schema. I understand that I can import an...
8
4804
by: Ferdi Smit | last post by:
I've never understood the rationale of allowing partial, but not explicit specialization for classes at non-namespace scope. Ie.: struct A { template <typename T1, typename T2> struct B {}; // this is not allowed: template <> struct B<int, float> {};
2
2474
by: Jeff S | last post by:
I have the following three lines at the very top of a class file (prior to the namespace declaration line). using System; using System.Data; using System.Data.SqlClient; Then, in a subsequent function in the same file I want to create a connection by entering the following line: SqlClient.SqlConnection cn = new
29
4243
by: Natan | last post by:
When you create and aspx page, this is generated by default: using System; using System.Collections; using System.Collections.Specialized; using System.Configuration; using System.Text; using System.Text.RegularExpressions; using System.Web; using System.Web.Caching;
5
10359
by: Christoffer | last post by:
Hi, I'm having trouble with namespaces when importing nodes. I'd like to get this output: <exampleRoot xmlns="http://mynamespace"> <row Lsm_Info="ABC123" /> <row Lsm_Info="DEF456" /> </exampleRoot> But instead I'm getting:
6
9009
by: John Kotuby | last post by:
Hi all, I am simply trying to include the Option Explicit declaration at the top of an ASP page and am getting an error: Error Type: Microsoft VBScript compilation (0x800A0400) Expected statement /transferkey.asp, line 2
1
1522
pbmods
by: pbmods | last post by:
Today marks the 85% mark of about a week of trying to learn SOAP, while the client is furious and demanding to know when his site will be done. I finally figured out why the server kept throwing NullReferenceExceptions in my face. The SOAP client I was using formatted its requests using explicit namespaces. E.g., <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://mynamespace.tld"> . .
1
4196
by: ARC | last post by:
Ok, this is strange. I created a new database and imported all objects from the old program file. Form some reason, the option explicit statement is no longer showing in my code modules (and I would assume any form modules as well). However, even though in the vb code window, under options,, the option "Require variable declaration" IS checked. Any ideas how to restore the Option Explicit option to all my modules? I never used to have...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10467
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10244
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.