Toplevel "test()" functions in standard library

Guido van Rossum guido at python.org
Wed Jul 26 22:01:37 EDT 2000


rob at hooft.net (Rob Hooft) writes:

> In the standard library there is about 20 modules with the construction:
> 
> ------------------------------------------
> [module definitions]
> 
> def test():
>     [testing code or general main program]
> 
> if __name__=="__main__":
>     test()
> ------------------------------------------
> 
> Wouldn't it be better to move the test() definition inside the if
> statement in these cases, to prevent name space pollution? Note that
> the routines are not called "_test()", so they will be imported by a
> "from ... import *" (There are other modules with a _test() definition
> as well).
> 
> Even worse: the test() routine might have severe side effects:
> e.g. base64.test() will block waiting for data from stdin...
> 
> Going one step further: Should all of these "test()" and "_test()"
> routines be in the code library at all, even now that we have a
> complete test engine?

As Moshe already said, the test code serves the reader of the module
well.  It's also handy to be able to import module foo and simply call
foo.test() or foo._test() to test it.

I agree that it's better to name the routine _test(), but for most
modules, and in most cases, it's bad style anyway to write ``from M
import *'', so naming it test() is no great sin.

--Guido van Rossum (home page: http://www.pythonlabs.com/~guido/)



More information about the Python-list mailing list