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

G

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

Daniel Nogradi

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
 
A

alisonken1

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?
 
G

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
 
B

Bjoern Schliessmann

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
 
B

Bart Willems

gtb said:
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.
 
J

John Machin

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<------------------------------------------------------
 
D

Duncan Booth

Bart Willems said:
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.
 
S

Steven D'Aprano

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:
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

%%%%%
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top