Use of __name__== ' __main__ ' in Python files and function methods in invoking other PY files

Source: Internet
Author: User

Recently learning Python often see a lot of py files have if _name_ = = '_main_': this sentence, so take some time to study, summed up the use of methods and principles.

Usually when we use the Python IDE to write the PY program is rarely used if name = = 'main': This sentence, because we run the PY file, not involved in the other directory custom py file, So you can execute the functions in the current PY file without having to write this sentence, but think about what if you need to invoke a custom function in another py file? This requires the use of the IF name = = 'main': up.

Example:

def a(x):    return x*2if __name__=="__main__":    print a(2#print 4

The single file used here runs with an output of 4, and the IF name= = "main" is used here: (the output is the same if you do not use this phrase)

If a function A is defined in the test1.py file and you want to invoke the A function in the test2.py file, then the IF name= = "main" is required:.
To illustrate:
test1.py

def a(x):    return x*2if __name__=="__main__":    print a(2)

test2.py

fromimport *print a(3#6

Here, in Test2, the custom function A in test1 is called, and the output is 6.
Here if the If name= = "main" is not used in test1: this sentence, the result of the output will be calculated one more time, because if there is no if name= = "main": The executable statements in the file will be executed in test2 (equivalent to no main function). If there is an if name= = "main": the equivalent of judging whether the Test1 file is a standalone test1 file or a custom function called in another file, here's an example of the second case, so the IF Name= = "main": The following statement will not be executed.

Also, here's how to invoke a custom function in the current file in another file?

For example, you need to call the C () custom function in the B file in the test folder of the sibling directory in Python file A, how do I implement it in a file?

fromimport *     #从文件路径为test.b的文件中导入所有的函数#调用b文件中的c函数

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use of __name__== ' __main__ ' in Python files and function methods in invoking other PY files

Related Article

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.