473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Huh?!?!? What is it that I'm seeing here?


First off, I'm a non-Python type person - I prefer a compiled language,
when the option is available, with a strong preference for C and/or C++.
(Sorry, boyz-n-girlz, I'm not interested in "You need to learn a real
language like _________" fights, or debates over the merits and/or
shortcomings of C/C++ - C/C++ is what I want the program I'm working
with to be written in, and that means that all discussion regarding
language choice is moot. End of discussion there.)

I'm in the process of trying to convert a Python script/program or
whatever the favored term is into C and/or C++, and of course, due to my
unfamiliarity with Python, I'm hitting some stuff that looks to be
idiomatic, without being able to locate a definition/description of the
idiom. Most of what I'm messing with seems pretty straightforward - it's
converting nicely into C++, and with my testing so far, appears to be
performing properly (albeit somewhat inefficiently, since I haven't done
any tweaking yet) but one particular construct has me stumped. It looks
like this:

if __name__ == '__main__':
run(argv[1:])

This (or a markedly similar construct) sits all by its lonesome self at
the bottom of several of the source files, apparently not part of any
class or declaration. It seems to be *FAIRLY* common in the group of
files that make up the program, which makes me think it may be some sort
of "preprocessor directive" type thing, similar to a C construct that
would conditionally compile in a "main()" routine if a given symbol
is/isn't defined. In all cases where this construct appears, there's a
corresponding "run()" method, which, confusingly, appears to be outside
the scope of any lexical blocks that I can recognize, so I've got no
contextual clue there...

I've paged through all the online Python docs I can find, but although I
recall seeing a reference to __name__ as some sort of special
variable/identifier, that was when I first came up against the language,
so I paid no attention at that time. Now, when I actually NEED it, I
can't LOCATE it! Can anybody help me out here? Judging by how common it
is in the files in this program, and the fact that I simply cannot
re-find the discussion of the "__name__" thing, I have to assume it's
one of those "Duh! Anybody smarter than a sheep already knows that!"
constructs that anyone with some experience with the language has
used/understands without needing more explanation.

Unfortunately, I have little-to-no experience with Python, and even less
interest in learning it - chances are so far beyond "high" that I'll
never write a single line of code in it that the number might as well be
infinite, since I have zero interest in taking the time to learn a new
language for a one-off project. I simply want a program that was
originally (however good, bad, or in-between the original decision might
have been) written in Python turned into a C/C++ program that I can work
with effectively using existing skills and knowledge. Not
knowing/understanding the function of this construct appears to be
hindering that process, so maybe somebody here can help me out?

Thanks in advance...

--
Don Bruder - da****@sonic.net <--- Preferred Email - unmunged, SpamAssassinated
Hate SPAM? See <http://www.spamassassin.org> for some seriously great info.
I will choose a path that's clear: I will choose Free Will! - N. Peart
Fly trap info pages: <http://www.sonic.net/~dakidd/Horses/FlyTrap/index.html>
Jul 18 '05 #1
6 1600
> performing properly (albeit somewhat inefficiently, since I haven't done
any tweaking yet) but one particular construct has me stumped. It looks
like this:

if __name__ == '__main__':
run(argv[1:])


This is used to check whether the script file is being
run directly as a python script (as opposed to being
imported as a module).

One use of this is to put unit tests in each module filed.
Then you can just do "python module.py" to run the code
under the if statement. When you import the module, the
test code will not be run.

Tobiah

Jul 18 '05 #2
In article <e0******************************@news.teranews.co m>,
Tobiah <to**@rcsreg.com> wrote:
performing properly (albeit somewhat inefficiently, since I haven't done
any tweaking yet) but one particular construct has me stumped. It looks
like this:

if __name__ == '__main__':
run(argv[1:])


This is used to check whether the script file is being
run directly as a python script (as opposed to being
imported as a module).

One use of this is to put unit tests in each module filed.
Then you can just do "python module.py" to run the code
under the if statement. When you import the module, the
test code will not be run.

Tobiah


BEAUTIFUL! Exactly the kind of information I was hoping for!

Looks like that means I can safely "lose" all occurrences, since once I
finish the port, it'll be a monolithic program.

Thanks Tobiah!

--
Don Bruder - da****@sonic.net <--- Preferred Email - unmunged, SpamAssassinated
Hate SPAM? See <http://www.spamassassin.org> for some seriously great info.
I will choose a path that's clear: I will choose Free Will! - N. Peart
Fly trap info pages: <http://www.sonic.net/~dakidd/Horses/FlyTrap/index.html>
Jul 18 '05 #3

"Don Bruder" <da****@sonic.net> wrote in message
news:Mj********************@typhoon.sonic.net...
I'm in the process of trying to convert a Python script/program or
whatever the favored term is into C and/or C++, and of course, due to my
unfamiliarity with Python, I'm hitting some stuff that looks to be
idiomatic, without being able to locate a definition/description of the
idiom. Most of what I'm messing with seems pretty straightforward - it's
converting nicely into C++, and with my testing so far, appears to be
performing properly (albeit somewhat inefficiently, since I haven't done
any tweaking yet) but one particular construct has me stumped. It looks
like this:

if __name__ == '__main__':
run(argv[1:])

This (or a markedly similar construct) sits all by its lonesome self at
the bottom of several of the source files, apparently not part of any
class or declaration. It seems to be *FAIRLY* common in the group of
files that make up the program, which makes me think it may be some sort
of "preprocessor directive" type thing, similar to a C construct that
would conditionally compile in a "main()" routine if a given symbol
is/isn't defined. In all cases where this construct appears, there's a
corresponding "run()" method, which, confusingly, appears to be outside
the scope of any lexical blocks that I can recognize, so I've got no
contextual clue there...
What's confusing you is that Python executes the module when it's
first run (or imported.) That means that any code outside of a def
is executed directly, including those last two lines that have
you puzzled.

"__main__" is the name of the module that is executed from the
command line, regardless of what its name was on disk. What
that means is that the if statement will succeed if the module is
run directly from the command line, but will fail if it is imported
from some other module.

HTH

John Roth

Thanks in advance...

--
Don Bruder - da****@sonic.net <--- Preferred Email - unmunged, SpamAssassinated Hate SPAM? See <http://www.spamassassin.org> for some seriously great info. I will choose a path that's clear: I will choose Free Will! - N. Peart
Fly trap info pages:

<http://www.sonic.net/~dakidd/Horses/FlyTrap/index.html>
Jul 18 '05 #4
>>>>> "Don" == Don Bruder <da****@sonic.net> writes:
Don> if __name__ == '__main__': run(argv[1:])

You can safely ignore these in your conversion project.

if __name__ == '__main__'

This means if the text file "somefile.py" holding the module code is
run as the main file, as in
python somefile.py or ./somefile.py

if the file has an executable bit set

run(argv[1:])

This means "call the function run, passing command line arguments as
arguments".

Basically, the code at the bottom allows any python module to save the
same purpose as the C/C++ function 'main'. Once you have converted
the python module, and implemented the function "run" in your C/C++
code, you would do something like

int main( int argc , char** argv ) {
run(argv);
}

Cheers,
John Hunter

Jul 18 '05 #5
On Sun, 14 Sep 2003 21:45:16 +0000 (GMT)
Don Bruder <da****@sonic.net> wrote regarding Huh?!?!? What is it that I'm seeing here?:
if __name__ == '__main__':
run(argv[1:])


The above code reads something like this: if the name of the current module is __main__, then invoke the run method with the parameter to run() being a list of the remaining arguments on the command line, excluding the argument that is the name of the python script to be executed.

The name of a module is set by Python to the value __main__ when the module is invoked from the command line. So if I have a command line like this:

python boink.py arg1 arg2 arg3

then the code for boink.py will be loaded and run. Since boink.py was loaded from the command line it will be given the module name __main__ and so the if statement will succeed. The run method will be invoked with argv[1:] expanded like this:

run(['arg1', 'arg2', 'arg3'])

(argv[0] is the value 'boink.py' from the above command line example.)

Hope that helps,
Bill

Jul 18 '05 #6
On Sun, 14 Sep 2003 22:32:05 GMT, Don Bruder <da****@sonic.net> wrote:

BEAUTIFUL! Exactly the kind of information I was hoping for!

Looks like that means I can safely "lose" all occurrences, since once I
finish the port, it'll be a monolithic program.


What you may want to do is to take the code executed by these idioms and
port them into your unit testing system. And I highly recommend unit
testing for any porting project.

Gary
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Johnny Ruin | last post by:
Hi, Running a trace while operating my application (written in C++ w/SQLDataProvider) I'm seeing all my queries wrapped in SET FMTONLY OFF; SET NO_BROWSETABLE ON;my query here;SET NO_BROWSETABLE...
5
by: Sue | last post by:
As soon as a character is entered in the first field of a new record, the number of records shown in the navigation buttons increases by 1 and the new record button becomes enabled. In the...
10
by: perryche | last post by:
Experts, I have the following scenario: Table1: RecordID RecordDate ... I want in a query to show me within this RecordID, only the latest RecordDate and info. I know I can use the Max...
0
by: walterb | last post by:
Hi, I’m new to C# and .NET, but here is my dilemma, In a nutshell my foreach loop is not seeing / addressing the first row of my dataset. foreach (GridViewRow row in...
2
by: parag_paul | last post by:
I have been seeing a consistent slowness in g++ compilation for a small file like the following , The uptime is near ( load Is near to 0 ) . I have put the time output for the same, The file...
2
by: raj | last post by:
Hi all, I'm using this code to import some a .txt files onto a webpage, but I keep seeing single quotes amd double quotes as question marks. Here's the code: <textarea disabled="disabled"...
31
by: Mark Dickinson | last post by:
On SuSE 10.2/Xeon there seems to be a rounding bug for floating-point addition: dickinsm@weyl:~python Python 2.5 (r25:51908, May 25 2007, 16:14:04) on linux2 Type "help", "copyright",...
0
by: franzbrown | last post by:
Most of the threads I am using in my C# application have names (Thread.Name = ). When I am debugging my application within VS 2005, I have a UI to stop my application and the threads it started....
0
by: kannu.valli3 | last post by:
Enjoy seeing with new video pictures http://nagaaraja.blogspots.com\
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.