Module default global variables for the Python standard library series

Source: Internet
Author: User

When we create a module of our own, the module itself comes with several global variables, which are present in each file.

View py Global variables for the current file

[[email protected] ~]# echo ' Print (VARs ()) ' > scripts.py [[email protected] ~]# python scripts.py {' __builtins__ ': <module ' __builtin__ ' (built-in), ' __name__ ': ' __main__ ', ' __file__ ': ' scripts.py ', ' __doc__ ': None, ' __package_ _ ': None}
Default global variables __file__ __package__ __cached__
Variable name Description
__doc__ Note for the py file, which is a comment on the file
Gets the current file path
Returns the package of the imported file, using . Delimited, if current file returns none
Return additional files imported PYC file path, current file returns none
_name_ If it is the primary file, return __main__ otherwise equals the module name

What's not listed is no need to know.

Instance

return scripts notes and file paths for files

[[email protected] ~]# cat scripts.py #!/usr/bin/env pyton# file Comment Shelf write "" "Python File Comments" "" # Output File Comment print ("comments", __doc__) # Output file path print ("File path", __file__)

Execute script

[[email protected] ~]# python scripts.py (' comments ', ' Python file Comments ') (' File path ', ' scripts.py ')

__name__Application of the function

View files and file contents under the current directory

[Email protected] ~]# Ls-ltotal 8drwxr-xr-x 2 root root 4096 may 21:36 lib-rw-r--r--1 root root and May 21:37 SC Ripts.py[[email protected] ~]# ls-l lib/total 12-rwxr-xr-x 1 root root Wu may 21:36 f1.py-rw-r--r--1 root root 0 M Ay 21:34 __init__.py[[email protected] ~]# cat lib/f1.py#!/usr/bin/env pythondef Echo (): Print ("lib/f1.py") [[Email Pro Tected] ~]# cat scripts.py #!/usr/bin/env pytonfrom lib import f1if __name__ = "__main__": F1.echo ()
Execute ' scripts.py ' script, the script will call ' lib/f1.py ' file, and ' lib/f1.py ' file will output ' lib/f1.py ', so the ' scripts.py ' file will naturally output ' lib/f1.py ' ' bash [[email protected] ~]# Python scripts.py # As we've seen, it will output the following content lib/f1.py

This is where we create a scripts1.py file again

[email protected] ~]# Touch scripts1.py

scripts1.pyThe file contents are as follows

[email protected] ~]# cat scripts1.py #!/usr/bin/env pytonimport scripts

File created well after executing him we'll find nothing output

[email protected] ~]# python scripts1.py

Then we change the contents of the scripts.py script file to the following

[[email protected] ~]# cat scripts.py#!/usr/bin/env pytonfrom lib import  f1f1.echo () 

scripts1.py Script This is going to output lib/f1.py

[email protected] ~]# python scripts1.py lib/f1.py

Why does the above situation occur?

Because the originalscripts.pyThe file adds the If judgment, which means that if__name__The value is equal to"__main__", then I'll execute the F1.echo () method, or else do nothing, whenscripts.pyWhen the file was directly executed by us.__name__return result is"__main__", if it is called by another file, then__name__The returned result is"scripts"

Automatically add the directory below the current directory bin to the Python PATH variable

Directory structure and script contents are as follows

[Email protected] ~]# tree/tmp//tmp/├──bin│├──f1.py│└──__init__.py└──scripts.py1 directory, 3 Files[[email Pro Tected] ~]# cat/tmp/scripts.py #!/usr/bin/env pytonimport osimport syspath = Os.path.dirname (__file__) Mod_path = Os.path . Join (Path, "Bin") Sys.path.append (Mod_path) for n in Sys.path:print (n)

Execution results

[Email protected] ~]# python/tmp/scripts.py/tmp/usr/lib64/python26.zip/usr/lib64/python2.6/usr/lib64/python2.6/ plat-linux2/usr/lib64/python2.6/lib-tk/usr/lib64/python2.6/lib-old/usr/lib64/python2.6/lib-dynload/usr/lib64/ python2.6/site-packages/usr/lib64/python2.6/site-packages/gtk-2.0/usr/lib/python2.6/site-packages/usr/lib/ Python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info/tmp/bin

This site is open source. Improve this page.


Module Default global variables for the Python standard library series

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.