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 .CompactGenerat or]
from CompactTest import CompactTest
class MaxQTest(Compac tTest):
# 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('MaxQT est').Run() 8 2314
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__':
.........whatev er............
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 .CompactGenerat or]
from CompactTest import CompactTest
class MaxQTest(Compac tTest):
# 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('MaxQT est').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
On Apr 27, 2:08 pm, gtb <goodTweetieB.. .@hotmail.comwr ote:
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 .CompactGenerat or]
from CompactTest import CompactTest
class MaxQTest(Compac tTest):
# 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('MaxQT est').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("MaxQT est").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?
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("MaxQT est").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
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
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.
On Apr 29, 9:32 pm, Bart Willems <b.r.will...@gm ail.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.farewe ll(tgt)
else:
# code used only in import mode
def farewell(whom):
print "Goodbye, %s" % whom
8<------------------------------------------------------
Bart Willems <b.*********@gm ail.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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 gets the same problem (y'all might
find it interesting too)...
********************************* Symptoms *********************************
When unserializing a previously serialized object, the __wakeup() function
is mysteriously called more...
|
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 PEP318.
Better get my facts straight first....
But if true that would seem to solve the main objection to:
|
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
Setup running, so all that is well and good.
My question concerns the ability to generically run scripts as if they
were being run on an installed copy of Python. Many scripts have the
following structure:
if __name__ == "__main__":
|
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 the best match. Anyone could give a
detail explanation in theory? Which one is good?
|
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 construction and then..
// some other processing and/or changing 'retval'
return retval;
}
| |
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 drag and drop works fine within the same application, but fails
|
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 quite tiresome to have to write code to call MyControl.Invoke for each
control on the form, along with the delegates that are required for each.
Is there a better way to do this? What I mean is, if I could marshal the
|
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;
...
return tempMoney;
|
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:
session_set_save_handler(array(&$sess_handler, 'open'),
array(&$sess_handler, 'close'), array(&$sess_handler, 'read'),
array(&$sess_handler, 'write'), array(&$sess_handler, 'destroy'),
array(&$sess_handler, 'gc'));
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |