473,505 Members | 15,976 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
8 2301
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2652
by: James Sleeman | last post by:
Hi all, i just spent an hour rifling through code to find the cause of a problem only to find it's an oddity with serialization and recursive objects, so figured I'd post for the next person who...
19
1783
by: Arthur | last post by:
Did I hallucinate something about __name__ becoming read-write? Not in alpha2. Can't find the reference to this I thought I read - that it was concluded to be necessary in connection with...
5
2738
by: Ed Leafe | last post by:
I'm working on creating a generic runtime engine for the Dabo framework. Right now I'm focusing on Windows, since many of our potential users are running on that platform. I've got py2exe and Inno...
11
2488
by: modemer | last post by:
If I define the following codes: void f(const MyClass & in) {cout << "f(const)\n";} void f(MyClass in) {cout<<"f()\n";} MyClass myclass; f(myclass); Compiler complain that it can't find...
25
2899
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
2
4480
by: Barry Moon | last post by:
Hi Can anyone give me any help with passing an object across processes, via drag and drop? I've written a custom ListView control, which supports dragging and dropping of its items. The...
4
3556
by: Charles Law | last post by:
Hi guys. I have two threads: a main thread and a background thread. Lots of stuff happens in the background thread that means I have to update several (lots) of controls on a form. It is...
11
1779
by: Xiaoshen Li | last post by:
Dear Sir, I am a little puzzled about a function returning a class object, for example, suppose I hava a class Money and a method: Money lastYear(Money aMoney) { Money tempMoney; ......
4
146684
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: ...
0
7218
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7478
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.