namespace question

is there a valid reason for moving from the old
from wxpython.wx import *
to the new
import wx
?
Should current wxpython projects consider moving to the new namespace or keep the old namespace instead?
what are the pros of the new namespace?

···

--
Peter Damoc
Hacker Wannabe
http://www.sigmacore.net/

The basic idea behind using import foo instead of from foo import * is
namespace cluttering, which can lead to importing functions,
procedures, classes, globals, etc., which have the same name, thus
leading to unpredictable behaviour and broken code, not to mention
your own custom functions, classes, etc. Also, it looks ugly, and can
clutter up the semantic namespaces inside of people's heads (when
reading your code).

···

On Thu, 26 Aug 2004 17:38:36 +0300, Peter Damoc <pdamoc@gmx.net> wrote:

is there a valid reason for moving from the old
from wxpython.wx import *
to the new
import wx
?
Should current wxpython projects consider moving to the new namespace or
keep the old namespace instead?
what are the pros of the new namespace?

Vladimir Gritsenko writes:

The basic idea behind using import foo instead of from foo
import * is namespace cluttering, which can lead to importing
functions, procedures, classes, globals, etc., which have the
same name, thus leading to unpredictable behaviour and broken
code, not to mention your own custom functions, classes, etc.
Also, it looks ugly, and can clutter up the semantic
namespaces inside of people's heads (when reading your code).

Exactly. To see this point, do the following:

import wx
len(dir(wx))

3143

dir(wx)

(pages and pages of identifiers flow by)

So, 3,143 names exist in the wx namespace. Now, that is a lot of
names, but at least I know that none of these names will ever
conflict with any of my names.

With the 'from wxPython import *' form, those 3,143 names go
into the global namespace, potentially conflicting with names
from other modules or from your own code. And even if they
don't conflict, it is certainly a bear to find your own needles
from inside that haystack.

From "The Zen of Python" by Tim Peters (see

http://www.python.org/doc/Humor.html#zen), the very last item
in the list is:

"Namespaces are one honking great idea -- let's do more of
those!"

···

--
Paul McNett
Independent Software Consultant
http://www.paulmcnett.com

Ok, but other than the namespace clutter, which can be overlooked if you have a good naming scheme, and the Tim Peters advice, what other reasons might there be?

···

On Thu, 26 Aug 2004 09:10:47 -0700, Paul McNett <p@ulmcnett.com> wrote:

Vladimir Gritsenko writes:

The basic idea behind using import foo instead of from foo
import * is namespace cluttering, which can lead to importing
functions, procedures, classes, globals, etc., which have the
same name, thus leading to unpredictable behaviour and broken
code, not to mention your own custom functions, classes, etc.
Also, it looks ugly, and can clutter up the semantic
namespaces inside of people's heads (when reading your code).

From "The Zen of Python" by Tim Peters (see
http://www.python.org/doc/Humor.html#zen), the very last item
in the list is:

"Namespaces are one honking great idea -- let's do more of
those!"

--
Peter Damoc
Hacker Wannabe

The replies to this so far have been absolutely
about avoiding ‘from X import *’ – as I can testify after commiting that
sin for so long and then having to rewrite all my import statements in
quite a large application.

However, so far as I can see, they don’t
answer the fundamental question that was asked: What is the value
of the new name space conventions?

Using the old name space conventions, one
does not need to say ‘from wxpython.wx import *’, and using the new name
space conventions you can say ‘from wx import *’. So the answer
doesn’t seem to lie in the direction of the answers given so far.

My guess is that the real answer lies somewhere
in the area of source and library organization for wxWindows. But
I’d be interested in more details as well.

···

Gary H. Merrill

Director and Principal Scientist, New Applications

Data Exploration Sciences

GlaxoSmithKline Inc.

(919) 483-8456

Peter Damoc wrote:

is there a valid reason for moving from the old
from wxpython.wx import *
to the new
import wx
?

* The namespace polution issues already discussed

* Needle in a haystack issues in tools that browse modules when looking at your own code.

* Using "from foo import *" is really frowned upon in the Python community

* Convenience. As has been mentioned you can avoid the namespace issue with the old code by using something like:

  from wxPython import wx
  f = wx.wxFrame(...)

but for a lot of people (myself included) the extra 'wx' before 'Frame' makes for more tedious coding and reduced readability of the code than using

  import wx
  f = wx.Frame(...)

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!