Python __ Function Cross-folder reference

Source: Internet
Author: User

1. __name__

Called module. __name is the module name of the called module, and if the function is executed directly, __name__ = the ' __main__ '.

The most widely used statements are: if __name__ = = ' main ':

The purpose of this statement is to enable the Py file to be self-executing and callable.

eg.py

def test ():

Print (' This is a test ')

if __name__ = = ' __main__ ':

Test ()

In this code, you can make the other py file call eg.py, because at this time __name__ is directly executed file name, if not execute, but also can call the test () method, at the same time, when eg.py directly executes, the __name__ is ' __main__ ', The If execution can be seen as the main function in Java;

2. __init__

__init__ usually appear in the PY in two ways: file, function. __init__ appears as a standalone py file in a Python file whose primary purpose is to separate the py file in the folder into a single package, which is contained in a separate PY package. In general, the __init__ file is empty, and next __init__ creates a new class as a function, unlike the __new__ operation, where the __NEW__ function returns an instance of that class. Details can be viewed: https://www.cnblogs.com/Lands-ljk/p/5880483.html

Here's a problem with Python cross-folder references in the VS code environment: The approximate structure is as follows:

Project

--app.py

--folder

Mod1

Mod2

In this case, the Mod1 module in the sibling folder is referenced by the app.py file under Project, and Mod1 has a reference to the MOD2, which requires the creation of an empty filein the folder ___init__.py, and the corresponding import statement to be modified. The code is as follows:

app.py:

From folder. MOD1 Import Mod1_service

Mod1_service ()

MOD1:

From folder. MOD2 Import Mod2_service

Def mod1_service ():

Mod2_service ()

MOD2:

Def mod2_service ():

Print (' MoD import success ')

If the folder is not in the Mod1 . Write all, you will find that mod2, seek reasons: through the import sys print (sys.path) to see when running app.py file to/project as a reference to the directory, will not enter the folder, this path needs to be labeled.

  

Python __ Function Cross-folder reference

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.