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

alisonken1 alisonken1 at gmail.com
Fri Apr 27 17:59:42 EDT 2007


On Apr 27, 2:08 pm, gtb <goodTweetieB... at hotmail.com> wrote:
> 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?
>

These are samples to give the programmer an idea of how the code is
supposed to work. If you cut/paste these examples in your working
code, you have to change the source to something that actually works.
BTW - Python does not use separate *.class files - *.py scripts (text
files) are byte compiled to *.pyc files for the python interpreter.
Would you perchance be referring to Java programming (which is a
different newsgroup)?


> 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()

In this case, the routine was called from a python command line, so
the __name__ variable is set to "__main__". This is a standard Python
trick to see if you are running the file as a stand alone script, or
if it was imported as a module (typically used with code that was
written that can be either standalone or used as part of a different
package - good for unit testing).

The example above { MaxQTest("MaxQTest").Run() } tells me either
you're trying to run a threaded application, or you're used to Java
programming.

Again, would you be wanting to talk to a Java newsgroup rather than a
Python newsgroup?




More information about the Python-list mailing list