import mysteries

David Abrahams dave at boost-consulting.com
Tue Jul 3 18:51:45 EDT 2007


on Thu Jun 21 2007, Ben Finney <bignose+hates-spam-AT-benfinney.id.au> wrote:

> David Abrahams <dave at boost-consulting.com> writes:
>
>> I'm pretty comfortable with Python, but recently I'm constantly
>> finding mysterious issues with import.  For example, looking at
>>
>>   http://genshi.edgewall.org/browser/trunk/genshi/filters/transform.py
>>
>> the examples use the symbol 'HTML' but it's not defined locally, it's
>> not explicitly imported, and there's no import *.  Yet doctest will
>> test this module and it passes with flying colors.  It turns out HTML
>> is defined in genshi.input.  How do I know that?  I grepped for it.
>> How does it become available to this module?
>
> That's a mystery to me too. I can't see by looking at the module where
> this 'HTML' name comes from, and as you say there is no import of
> 'genshi.input'.
>
> Whatever the explanation, it's a violation of one of the strengths of
> Python: namespaces. Names that appear in the current namespace (as
> happens with 'from foo import *', and as seems to be happening here)
> are bad programming style, for exactly the reason that they make the
> code more difficult to understand.

Sometimes that's true, but I disagree with it as a general rule.  Some
bindings are idiomatic, in widespread use throughout a project, and
would look dumb and make the code too verbose and hard-to-understand
if qualified (especially if fully qualified).

The practice may make it harder to understand when something goes wrong, but
that's a separate issue.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

The Astoria Seminar ==> http://www.astoriaseminar.com




More information about the Python-list mailing list