import statement convention

Steve Holden steve at
Wed Apr 9 04:35:33 CEST 2008

Ben Finney wrote:
> MartinRinehart at writes:
>> By convention, I've read, your module begins with its import
>> statements. Is this always sensible?
> There are exceptions, but the benefits are great: It's very easy to
> see what this module requires, without needing to execute it.
>> I put imports that are needed for testing in the test code at the
>> end of the module. If only a bit of the module has a visual
>> interface, why pollute the global namespace with 'from Tkinter
>> import *'? Wouldn't that be better done in a separate class or
>> function?
> If your test code requires the Tkinter module but the rest of the code
> doesn't, why pollute the functional module with such test code? Move
> it to a separate unit test module.
If your test code is the only part of the module that needs Tkinter I'd 
be perfectly happy seeing the import guarded by the if __name__ == 
"__main__" condition.

Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC    

More information about the Python-list mailing list