Guidance on initialization code in a module

Discussion in 'Python' started by mrstevegross, Jun 16, 2009.

  1. mrstevegross

    mrstevegross Guest

    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?

    Thanks,
    --Steve
     
    mrstevegross, Jun 16, 2009
    #1
    1. Advertising

  2. mrstevegross

    Dave Angel Guest

    mrstevegross 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?
    >
    > Thanks,
    > --Steve
    >
    >

    You're doing fine. Everything in the module is run when it's imported.
    Running a definition, of course, compiles it, and adds its name to the
    module's namespace. But anything outside of a definition or class, or
    other qualifier will just be run.

    If the module will never be used as a script, the if __name__ == logic
    isn't needed. If you want this initialization code to be run *only* if
    it's not being run as a script, then you'd do as you suggest, if
    __name__ != "__main__"

    But usually, the initialization code wants to be run whether it's being
    used as a script, or as a library module. So leave the conditional off,
    till you actually decide what behavior should be conditional.
     
    Dave Angel, Jun 16, 2009
    #2
    1. Advertising

  3. mrstevegross

    David Stanek Guest

    On Tue, Jun 16, 2009 at 4:54 PM, mrstevegross<> 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
     
    David Stanek, Jun 16, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    10
    Views:
    948
  2. Matthias Kaeppler
    Replies:
    2
    Views:
    446
    Victor Bazarov
    Jul 18, 2005
  3. Replies:
    6
    Views:
    464
    Ron Natalie
    Dec 11, 2005
  4. toton
    Replies:
    5
    Views:
    935
    Victor Bazarov
    Sep 28, 2006
  5. Jess
    Replies:
    23
    Views:
    932
Loading...

Share This Page