noob import question

Ben Finney bignose+hates-spam at benfinney.id.au
Fri May 19 21:15:33 EDT 2006


[Please don't top-post. Please don't indiscriminately quote the entire
message you respond to.
<URL:http://en.wikipedia.org/wiki/Top_posting>]

Brian Blazer <brian at brianandkate.com> writes:

> Thank you for your responses.  I had a feeling is had something to
> do with a namespace issue but I wasn't sure.

Another point to note is that 'from foo import *' is bad practice. It
causes a problem known as "namespace pollution", where you can't tell
where a particular name you're using comes from, or if you have
accidentally clobbered an existing name.

Either import the module to a single name, so you can qualify the
names inside that module:

    import foo
    import awkward_long_module_name as bar
    foo.eggs()
    bar.beans()

Or, if you only need a few objects from the module, import those
specifically and use their names directly:

    from foo import spam, eggs
    eggs()
    spam()

> You are right, I do come from a Java background.  If it is poor form
> to name your class file the same as your class, can I ask what the
> standard is?

Since we don't need to have one file per class, the convention is to
group your modules by coherent functionality. Place any names,
functions, classes, whatever that all relate to a discrete, coherent
set of functionality in a single module.

The Python coding guidelines[0] also recommend that the module be
named all in lower-case, but that's more about consistency within your
own code base.

[0]: <URL:http://www.python.org/dev/peps/pep-0008>

-- 
 \       "I have a map of the United States; it's actual size. It says |
  `\   '1 mile equals 1 mile'... Last summer, I folded it."  -- Steven |
_o__)                                                           Wright |
Ben Finney




More information about the Python-list mailing list