Guidance on initialization code in a module

David Stanek dstanek at dstanek.com
Tue Jun 16 18:40:44 EDT 2009


On Tue, Jun 16, 2009 at 4:54 PM, mrstevegross<mrstevegross at gmail.com> wrote:
> Is there a common way to initialize various stuff in a module? That
> is, I have some code in my module that I want to run whenever the
> module is imported. Currently, my module looks like this:
>
> === foo.py ===
> def something():
>  ...
>
> def somethingelse():
>  ...
>
> something()
> === EOF ===
>
> Is the 'something()' line at the end in an ok location? I just put it
> at the end. Maybe there's some special __init__() mechanism for
> modules? Or should I use the 'if __name__ != '__main__'' trick?
>

I think what you are doing is fine. The only thing that I would do
differently is rename 'something' to 'initialize'. That way your
intent is really obvious.


-- 
David
blog: http://www.traceback.org
twitter: http://twitter.com/dstanek



More information about the Python-list mailing list