i have written some code in test.py as below:
import __main__
if __name__!='__main__':
print 1
print 2
when i run test.py, i got
2
on the screen.
now, i have some question about the code, 1. since no __main__ module at
all, why it's legal to write "import __main__"?
2. since if running a script independently, the __name__ should be
'__main__', why it's not in the above code?
thank you in advance.
with my kind regards,
Wen 2 4536
wen wrote: i have written some code in test.py as below: import __main__ if __name__!='__main__': print 1
print 2
when i run test.py, i got 2 on the screen.
now, i have some question about the code, 1. since no __main__ module at all, why it's legal to write "import __main__"?
Because a module object is created for the running script. It is placed
in sys.modules with the name "__main__". When you "import __main__", the
import mechanism looks in sys.modules first for a module with the name
"__main__" and uses that if it's there.
2. since if running a script independently, the __name__ should be '__main__', why it's not in the above code?
It is. However, you wrote "!=" instead of "==".
--
Robert Kern rk***@ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
> import __main__ if __name__!='__main__': print 1
print 2
when i run test.py, i got 2 on the screen.
now, i have some question about the code, 1. since no __main__ module at all, why it's legal to write "import __main__"?
__main__ is the module that the interpreter starts executing. So
"import __main__" is legal, though I'm guessing it doesn't really do
anything because the module is already loaded (since it's running).
2. since if running a script independently, the __name__ should be '__main__', why it's not in the above code?
But it is. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Amy G |
last post by:
I have a program that needs a little help.
Right now the program runs in my crontab. When it runs, it sets a few
variables based on a query to a MySQL database. I would like to modify it
so that...
|
by: Vio |
last post by:
Hi,
I've been trying to embed (statically) wxPy alongside an embedded py
interpreter on a linux/gtk box.
At one point, for some reason misc.o linking reported "multiple
definitions of...
|
by: Bill Davy |
last post by:
I am working with MSVC6 on Windows XP.
I have created an MSVC project called SHIP
I have a file SHIP.i with "%module SHIP" as the first line (file is below).
I run SHIP.i through SWIG 1.3.24...
|
by: wen |
last post by:
and, in which case, the following case will happen:
if __name__!='__main__':
do_sth()
any help would be appreciated.
|
by: Vitali Gontsharuk |
last post by:
Hi!
I can't find an answer to the following question: does the "import"
command in XML Schema import also element declarations? Or only type
definitions? As far as I understood, the first takes...
|
by: Mudcat |
last post by:
I have a directory structure that contains different modules that run
depending on what the user selects. They are identical in name and
structure, but what varies is the content of the functions....
|
by: johnny |
last post by:
What is the purpose of
if __name__ == "__main__":
If you have a module, does it get called automatically?
|
by: Eric Hanchrow |
last post by:
(This is with Python 2.5.2, on Ubuntu Hardy, if it matters.)
This seems so basic that I'm surprised that I didn't find anything
about it in the FAQ. (Yes, I am fairly new to Python.)
Here are...
|
by: J. Cliff Dyer |
last post by:
On Thu, 2008-05-08 at 12:00 -0700, Eric Hanchrow wrote:
It's the same reason as this:
5
5
6
5
Python "variables" are just names that point at objects. When you
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |