if __name__ == 'main': & passing an arg to a class objec

Daniel Nogradi nogradi at gmail.com
Fri Apr 27 23:55:33 CEST 2007

> The lines
> if __name__ == 'main':
>    someClass().fn()
> appear at the end of many examples I see. Is this to cause a .class
> file to be generated?

Python doesn't generate .class files, and the example you mean is
probably more like

if __name__ == '__main__':

which causes the whatever block to be executed when the program is run
from the command line (as opposed to being imported).

> The last line of the sample below has a string parameter. When I
> mimicked this I got an error stating that the class constructor did
> not take an arg, which seems correct.
> Thanks,
> gtb
> # Generated by MaxQ [com.bitmechanic.maxq.generator.CompactGenerator]
> from CompactTest import CompactTest
> class MaxQTest(CompactTest):
>     # Recorded test actions.
>     def runTest(self):
>         self.msg('Test started')
>     # ^^^ Insert new recordings here.  (Do not remove this line.)
> # Code to load and run the test
> if __name__ == 'main':
>     MaxQTest('MaxQTest').Run()

It's hard to say what MaxQTest takes as an argument without seeing the
code. If you post more details it might be easier to help you, but in
any case this may be useful: http://python.org/doc/tut/


More information about the Python-list mailing list