By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

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

P: n/a
gtb
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?
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()

Apr 27 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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__':
.........whatever............

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/

Daniel
Apr 27 '07 #2

P: n/a
On Apr 27, 2:08 pm, gtb <goodTweetieB...@hotmail.comwrote:
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?

Apr 27 '07 #3

P: n/a
gtb
Sorry for the wrong implication. I should have said I 'mimicked the
style'.

No, not used to Java at all, and obviously not versed in python either
(do I get points for tcl?). Maxq generates jython scripts and when I
saw the .class files I assumed it was the work of the python compiler
as what is visible to me appears to be python syntax. The example
above { MaxQTest("MaxQTest").Run() } is actually par of what is
generated by maxq first rattle out of the box when you specify a
compact script. Since I am a neophyte to java, maxq, and python, what
is what is not clear to me and I apologize for imposing on the the
forum with this mixed bag of problems. However, I have learned a great
deal (to me) of python in the past couple of days, just gotta break
the tcl and autoIt habit of dollarSigning things.

RE: http://python.org/doc/tut/, I keep it and http://docs.python.org/ref/ref.html
both open all the time. :^)

Best Regards,

gtb

Apr 27 '07 #4

P: n/a
alisonken1 wrote:

[if __name__ == "__main__"]
These are samples to give the programmer an idea of how the code
is supposed to work.
No, this belongs into comments or docs. The contents of this block
are often used for testing or debugging, or for normally executable
code if it makes sense to call the module directly.

Regards,
Björn

--
BOFH excuse #224:

Jan 9 16:41:27 huber su: 'su root' succeeded for .... on /dev/pts/1

Apr 28 '07 #5

P: n/a
gtb wrote:
appear at the end of many examples I see. Is this to cause a .class
file to be generated?
This might be obvious, but no one else mentioned it: the Python
interpreter cannot execute code that it hasn't compiled yet, which is
why the "if __name__ ..." code is always at the end of the module - to
guarantee that the entire file is scanned first.
Apr 29 '07 #6

P: n/a
On Apr 29, 9:32 pm, Bart Willems <b.r.will...@gmail.comwrote:
gtb wrote:
appear at the end of many examples I see. Is this to cause a .class
file to be generated?

This might be obvious, but no one else mentioned it: the Python
interpreter cannot execute code that it hasn't compiled yet, which is
why the "if __name__ ..." code is always at the end of the module - to
guarantee that the entire file is scanned first.
I make no claims about the following code; it is merely presented for
examination :-)

8< --- namemain.py -----------
# code used both in import mode and script mode
def greet(whom):
print "Hello, %s" % whom
if __name__ == "__main__":
# code used only in script mode
import sys, namemain
tgt = sys.argv[1]
greet(tgt)
namemain.farewell(tgt)
else:
# code used only in import mode
def farewell(whom):
print "Goodbye, %s" % whom
8<------------------------------------------------------
Apr 29 '07 #7

P: n/a
Bart Willems <b.*********@gmail.comwrote:
gtb wrote:
>appear at the end of many examples I see. Is this to cause a .class
file to be generated?
This might be obvious, but no one else mentioned it: the Python
interpreter cannot execute code that it hasn't compiled yet, which is
why the "if __name__ ..." code is always at the end of the module - to
guarantee that the entire file is scanned first.
This is somewhat misleading: the entire file is always compiled before any
of it is executed (to see this try putting a syntax error on the last line:
the syntax error will prevent any lines in the file being executed).

A more accurate statement would be to say that executing code cannot access
any names which have not yet been defined. The code for functions and
classes is compiled first with everything else, but the function or class
objects are not created, and the relevant names are not bound until the
appropriate 'def' or 'class' statement is executed.
Apr 30 '07 #8

P: n/a
On Sun, 29 Apr 2007 07:32:44 -0400, Bart Willems wrote:
gtb wrote:
>appear at the end of many examples I see. Is this to cause a .class
file to be generated?
This might be obvious, but no one else mentioned it: the Python
interpreter cannot execute code that it hasn't compiled yet, which is
why the "if __name__ ..." code is always at the end of the module - to
guarantee that the entire file is scanned first.
Nonsense.

Here's my "test.py":

%%%%%

x = 42

if __name__ == "__main__":
print "x has value", x
x = 23

print "now x has value", x

%%%%%

It works just as you would expect:
$ python test.py
x has value 42
now x has value 23

And when you import it:
>>import test
now x has value 42
There is nothing, absolutely nothing, magic about the idiom
if __name__ == "__main__". It is just an if block, like any other if
block. If you still aren't convinced, try this one:
%%%%%

x = 42

if __name__ == "__main__":
print "x has value", x
print "y has value", y

y = 43

%%%%%

--
Steven D'Aprano

May 1 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.