Explicit or general importing of namespaces?

Discussion in 'Python' started by Harlin Seritt, Feb 28, 2005.

  1. 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
     
    Harlin Seritt, Feb 28, 2005
    #1
    1. Advertising

  2. > 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
     
    Diez B. Roggisch, Feb 28, 2005
    #2
    1. Advertising

  3. 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
     
    Michael Hoffman, Feb 28, 2005
    #3
  4. Harlin Seritt

    Max M Guest

    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
     
    Max M, Feb 28, 2005
    #4
  5. > 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
     
    Diez B. Roggisch, Feb 28, 2005
    #5
  6. Harlin Seritt

    Peter Hansen Guest

    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
     
    Peter Hansen, Feb 28, 2005
    #6
  7. Harlin Seritt

    Paul Rubin Guest

    Peter Hansen <> 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".
     
    Paul Rubin, Feb 28, 2005
    #7
  8. Paul Rubin <http> wrote:
    > Peter Hansen <> 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 <> -- http://www.craig-wood.com/nick
     
    Nick Craig-Wood, Mar 1, 2005
    #8
  9. Harlin Seritt

    Peter Mayne Guest

    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
     
    Peter Mayne, Mar 1, 2005
    #9
  10. Harlin Seritt

    Peter Hansen Guest

    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
     
    Peter Hansen, Mar 4, 2005
    #10
  11. 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" <> wrote in message
    news:...
    > 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
     
    Harlin Seritt, Mar 5, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ravi sankar
    Replies:
    2
    Views:
    387
    abdul bari
    Aug 27, 2003
  2. D. Shane Fowlkes
    Replies:
    1
    Views:
    938
    Tu-Thach
    Jan 13, 2004
  3. J.T. Conklin
    Replies:
    1
    Views:
    444
    David Hilsee
    Aug 11, 2004
  4. Andy
    Replies:
    5
    Views:
    512
    Shezan Baig
    Jan 30, 2005
  5. Replies:
    1
    Views:
    583
    Salt_Peter
    Dec 25, 2006
Loading...

Share This Page