In Python, if _ name _ = "_ main _" is explained in detail, __name ___ main __

Source: Internet
Author: User

In Python, if _ name _ = "_ main _" is explained in detail, __name ___ main __

Many python users will see such a statement for the first time. if _ name _ = "_ main __":

So what is the purpose of this statement? Before explaining this, we must first declare that no matter how small you are, what you must know is:

1. The extension of the python file is. py;

2. The. py file can be directly executed.Like a small program, it can also be used as a module to be imported. (For example, 360 Security Guard relies on functional modules. For example, the 360 security guard framework is a desktop, the icons above are shortcuts, which point to these functional modules)

3. The import module in python generally uses import

After confirming the preceding points, you can start to explain the if _ name _ = "_ main _": Statement.

First, explain if. As the name suggests, if means if. if is added at the beginning of the sentence, it indicates that this sentence is a conditional statement. It is very easy to learn how to use the if statement. Of course, a lot of practice is required to be used flexibly.

The next step is _ name __,__ name _ as the built-in attribute of the module. Simply put, it is the call method of the. py file.

Finally, it is _ main __. I mentioned earlier that the. py file can be called as a module or used directly. If it is equal to "_ main _", it indicates direct execution.

Conclusion: When the if _ name _ = "_ main _": after the statement is called as a module, the code after the statement is not executed; code execution after a statement is directly used. Generally, this statement is used in a module test.


Python question if _ name _ = '_ main __':

First answer __: because python class members are both public and publicly accessible, they lack private attributes like the Orthodox object-oriented language. So we will use _ to simulate private attributes. These _ attributes are often used internally and generally do not need to be rewritten. No need to read. The purpose of adding two underscores (_) is not to conflict with the names of common public properties, or to prevent users (non-developers) of objects from using them at will.

Object-oriented is an improvement in methodology. You can use modules without objects, just as the C language provides modules in a similar way without objects, and you can develop anything. However, the use of objects can improve code reuse, improve development efficiency, and reduce developers' labor. How to make good use of objects involves a wide range of books. Let me explain it to you.
It's like building a plaster sculpture in a factory.
The class is equivalent to the mold. The object is equivalent to a sculpture from the mold.
A large number of sculptures can be copied. To modify a sculpture, modify the mold instead of the finished sculpture.
From the data point of view, a sculpture may consist of multiple basic components, including hands, feet, and heads. These are data.
Of course, an object also has actions. All functions of an object are its actions, such as running, eating, and calling.
The most common is student, which specifies some data, such as name, phone number, age, and data.
"Answer", "eat", "consume", "Contact", and "hands-on" are the actions for passing messages.
This combination of data and actions is an object. Classes are used to regulate the data and actions. If you use the same class object again, you do not need to repeat development.

In PYTHON, if _ name _ = "_ main _": What is it?

_ Name _ indicates the method of calling the current py file. If it is equal to "_ main _", it indicates direct execution. if it is not, it is called by other files. if it is False at this time, then it will not execute code on the outermost layer.
For example, if you have a Python file
Def XXXX ():
# Body
Print "asdf"
In this case, even if the file is imported elsewhere, the XXXX function will be called and print "asdf" will be executed because it is the outermost code or global code. But we often want to run some code only when I execute this file. If it is not (that is, if it is called), we will not execute the code.
Def XXXX ():
# Body
If _ name __= "_ main __":
Print "asdf"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.