Good practice when writing modules...

John Machin sjmachin at
Sun Nov 16 10:32:59 CET 2008

On Nov 16, 6:58 pm, bearophileH... at wrote:
> John Machin:
> > > import foo # used by baz()
> > > import bar # used by spam()
> > Why bother with the ()?
> I code in other language too beside Python, in those languages there
> are other things (like templates in D language) beside functions, so
> my comment helps me remember that baz() is a function instead of a
> template written in lowercase: baz!().
> > Why bother at all?
> If I remove a function/class that uses baz from the module, I can go
> at its top and find in a short time what imports to remove.

pychecker tells you about modules that are imported but not used.

> Probably
> there are other purposes too.
> > Do you remember to adjust the comment when a function/class is changed
> > so that it doesn't use the module any more?
> Most of the times yes. Even if I forget doing it 2%-5% of the times,
> it's worth it, for me. Notice that I have just shown what coding
> practices seem currently good for me, for certain kinds of programs,
> etc. Feel free to use your own.
> > Your modules are callable???
> No, they aren't, it's a mistake of mine, sorry.
> > > Time ago I have read an interesting article that says that comments of
> > > today will often become statements and declaration and tags of
> > > tomorrow, that is stuff that the interpreter/compiler/editor
> > > understands. This means that such annotations of mine may be something
> > > fit to become a syntax of the language.
> > I hope not.
> Can you explain why you don't like my idea?

Yes. The vast majority of imports are unconditional and not done
dynamically via the __import__ built-in function and all the
information required about what function/class/method needs what
module to be imported is ALREADY in your source file. If you want to
faff about duplicating that in comments, feel free. However suggesting
that this be enshrined in language syntax is mind -boggling.

More information about the Python-list mailing list