Good practice when writing modules...

Robert Kern robert.kern at gmail.com
Sat Nov 15 17:41:21 EST 2008


r0g wrote:

> The module I am compiling is kind of a scrapbook of snippets for my own
> development use, it has no coherent theme and I wouldn't be distributing
> it or using the whole thing in a production environment anyway, just
> copying the relevant functions into a new module when needed. I'm
> thinking having the imports inline might make that process easier when I
> do need to do it and once copied I can always move them out of the
> functions declarations.

This is something of a special case, so I don't think the usual style guides 
entirely apply. What I would do is keep the imports outside of the functions, 
but put them before the functions that use them. Repeat the imports as 
necessary. For example, if f() uses modules foo and bar, while g() uses only foo:

############################

import foo
import bar

def f():
     ...

############################

import foo

def g():
     ...

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list