Should I put the main in a class in Python?
<p>So far I've running the <code>main()</code> module in a script way (different .py file and not in a class or whatever) and then call the different instances from my other modules.</p>

<p>Is this right or should I make a class just for the <code>main</code> too?</p>

<p>Regards and thank.</p>

<p>You're overthinking it. Don't shoe-gaze over stuff that doesn't have a clear purpose. If you find a good reason to put a "main" function in a class, go ahead. Otherwise just save yourself the trouble.</p>

<p>To answer your question directly, I've never seen the "<code>main</code>" method defined in a <code>class</code> in python. The "main" function is usually just a driver for whatever you want to do with your program. It's something you want to execute every time your program is called. There's no general reason to represent this as an abstract data type, so I don't see the sense in encapsulating it in a class.</p>

<p>The python idiom I've seen most is to check <code>if __name__ == "__main__":</code>, which allows you to run a block of code only if the module is called directly, not as an <code>include</code> to another file. For example:    </p>

<pre><code>if __name__ == "__main__":
    print "This module was called directly."
</code></pre>

<p>You can see the question <a href="https://stackoverflow.com/questions/419163/what-does-if-name-main-do">What does <code>if __name__ == â__main__â:</code> do?</a> for more details.</p>

python