---restore content starts---
VARs () view built-in global variables
Returning a built-in global variable in a dictionary
# !/usr/bin/env python # -*-Coding:utf8-*- Print (VARs ()) # Output # {' __builtins__ ': <module ' builtins ' (built-in);, ' __spec__ ': None, ' __package__ ': None, ' __doc__ ': none, ' __n ame__ ': ' __main__ ', ' __cached__ ': None, ' __file__ ': ' h:/py/index.py ', ' __loader__ ': <_frozen_importlib_external. Sourcefileloader object at 0x000000ac32c66a58>}
__doc__ : Get comments for a file
# !/usr/bin/env python # -*-Coding:utf8-*- """ here is the comment of the file """ Print (__doc__) # __doc__ : Get comments for file # output # Here is the comment for the file
__file__ "Focus" gets the path to the current file
# !/usr/bin/env python # -*-Coding:utf8-*- Print (__file__) # __file__: Gets the path to the current file # Output # h:/py/index.py
Will often use __file__, usually with the OS module os.path.dirname (), Os.path.basename (), Os.path.join () module functions to use
#!/usr/bin/env python#-*-Coding:utf8-*-ImportOSA=__file__ #__file__ global variable gets the current file pathPrint(a) b= Os.path.dirname (a)#get file current directory: note: os.path.dirname () overlay once to find the followingPrint(b) B2= Os.path.dirname (b)#gets the parent directory of the current directory of the file, note: os.path.dirname () overlay once to find oncePrint(B2) C= Os.path.basename (a)#Get file namePrint(c)#Output#h:/py/lib/ska/mk.py#H:/py/lib/ska#H:/py/lib#mk.py
__PACKAGE__: Get the path of the import file, multi-level directory with point segmentation, note: Return none for the current file
#!/usr/bin/env python#-*-Coding:utf8-*-Print(__package__)#Note: None is returned for the current file fromLib.skaImportMk#Import the Mk module filePrint(Mk.__package__)#__package__: Get the path of the import file, multi-level directory with point segmentation, note: Return none for the current file#Output#None#Lib.ska#Lib.ska
__CACHED__: Gets the cache path of the import file
# !/usr/bin/env python # -*-Coding:utf8-*- from Import Mk # Imports MK module file print(Mk.__cached__#__ CACHED__: Gets the cache path of the import file # output # H:\py\lib\ska\__pycache__\ MK.CPYTHON-35.PYC
__name__ "Focus" gets the path of the import file plus the file name, the path is split with points, note: Gets the current file returned __main__
#!/usr/bin/env python#-*-Coding:utf8-*-Print(__name__)#Note: Get the current file back __main__ fromLib.skaImportMk#Import the Mk module filePrint(Mk.__name__)#gets the path of the import file plus the file name, with the path separated by points#Output#__main__#lib.ska.mk
The __NAME__ global variable is written in the portal file, only the return value when executing the portal file is __main__, if the import file is imported into another file, the __name__ return value of the portal file is not __main__. Instead, if the file's path joins the port file name, we can use the __name__ global variable to prevent others from hotlinking the portal file
# !/usr/bin/env python # -*-Coding:utf8-*- if __name__ " __main__ " # The __name__ global variable is written in the portal file, only the return value when executing the portal file is __main__ , if the import file is imported into another file, the __name__ return value of the portal file is not __main__. Instead, if the file's path joins the port file name, we can use the __NAME__ global variable to prevent others from hotlinking the import file print(" execute " # This is done only when the index file is executed, and index is imported to another file, it will not be executed in the Judgment
---restore content ends---
39th, Python built-in global variables