When we import a module into a file , Imported are those names in the module that are not underlined ( Underline “_” Or double underline “__”) Variable at the beginning 、 Functions and classes . therefore , If we don't want a member of the module file to be introduced into other files , You can underline it before its name .
Created demo.py Module files and test.py File as an example ( They are in the same directory ), Their respective contents are as follows :
#demo.py
def say():
print(" Life is too short , I learned Python!")
def CLanguage():
print("C Chinese language network :http://c.biancheng.net")
def disPython():
print("Python course :http://c.biancheng.net/python")
#test.py
from demo import *
say()
CLanguage()
disPython()
perform test.py file , The output is :
Life is too short , I learned Python!
C Chinese language network :http://c.biancheng.net
Python course :http://c.biancheng.net/python
On this basis , If demo.py Module disPython() The function does not want other files to introduce , Just change its name to _disPython() perhaps __disPython(). After the modification , Re execution test.py, The output is :
Life is too short , I learned Python!
C Chinese language network :http://c.biancheng.net
Traceback (most recent call last):
File "C:/Users/mengma/Desktop/2.py", line 4, in <module>
disPython()
NameError: name 'disPython' is not defined
obviously ,test.py You cannot use an unimproved... In a file disPython() function .
besides , You can also use the __all__ Variable , The value of this variable is a list , It stores some members of the current module ( Variable 、 Function or class ) The name of . By setting... In the module file __all__ Variable , When other documents are in the form of “from Module name import *” When importing the module in the form of , Only... Can be used in this file __all__ Members specified in the list .
in other words , Only with “from Module name import *” Module of formal import , When the module is equipped with __all__ variable , Only members specified by this variable can be imported , Members that are not specified cannot be imported .
for instance , modify demo.py The code in the module file :
def say():
print(" Life is too short , I learned Python!")
def CLanguage():
print("C Chinese language network :http://c.biancheng.net")
def disPython():
print("Python course :http://c.biancheng.net/python")
__all__ = ["say","CLanguage"]
so ,__all__ The variable contains only say() and CLanguage() The function name of , It doesn't contain disPython() Name of function . In this case, execute directly test.py file , The implementation result is :
Life is too short , I learned Python!
C Chinese language network :http://c.biancheng.net
Traceback (most recent call last):
File "C:/Users/mengma/Desktop/2.py", line 4, in <module>
disPython()
NameError: name 'disPython' is not defined
obviously , about test.py The file is ,demo.py Module disPython() The function is not introduced , Such a call is illegal .
Once again ,__all__ Variables are limited to other files with “from Module name import *” Way to introduce . in other words , If you use the following 2 There are two ways to introduce modules , be __all__ Variable setting is invalid .
1) With “import Module name ” Import module in the form of . After importing the module in this way , You can always use the module name prefix ( If an alias is specified for the module , You can use the alias of modkuai as the prefix ) To call all members of the module ( Except for members named at the beginning of the underline ).
Still with demo.py Module files and test.py File as an example , The code to modify them is as follows :
#demo.py
def say():
print(" Life is too short , I learned Python!")
def CLanguage():
print("C Chinese language network :http://c.biancheng.net")
def disPython():
print("Python course :http://c.biancheng.net/python")
__all__ = ["say"]
#test.py
import demo
demo.say()
demo.CLanguage()
demo.disPython()
function test.py file , The output is :
Life is too short , I learned Python!
C Chinese language network :http://c.biancheng.net
Python course :http://c.biancheng.net/python
You can see , although demo.py Set in module file __all__ Variable , But when “import demo” After the method is introduced ,__all__ Variables will not work .
2) With “from Module name import member ” Import the specified members directly in the form of . Modules imported in this way ,__all__ Variable setting , It's just like nothing .
Still with demo.py and test.py For example , modify test.py Code in file , As shown below :
from demo import say
from demo import CLanguage
from demo import disPython
say()
CLanguage()
disPython()
function test.py, The output is :
Life is too short , I learned Python!
C Chinese language network :http://c.biancheng.net
Python course :http://c.biancheng.net/python