directory

  • A brief introduction.
    • 1.__name__
    • 2. ‘__main__’
    • 3. The __name__ = = “__main__”
  • Role.
  • Guess you like it

Recommended path for learning Python: Python Learning Directory >> Python Basics

Those of you who have studied C or Java probably know that programs run with a main entry. Python, on the other hand, can run a block of code from top to bottom even without a main entry. __name__ == “__main__” is still used in many Python open source projects or modules.

oneIntroduction to the

1.__name__

** __name__** is a Python built-in property and is a system global variable! Each py file has its own __name__ :

** If the py file is imported as a module, __name__ is the filename of the py file (also known as the module name); arunachal

** If the py file is run directly (Ctrl+Shift+F10), then __name__ defaults to the string “__main__”; arunachal

Take a simple example: suppose your name is Zhang SAN

In the eyes of friends, you are Zhang SAN (__name__ == ‘Zhang SAN ‘);

In your own eyes, you are yourself (__name__ == ‘__main__’)

2. ‘__main__’

‘_ _main_ _’ is actually onestring, used to identify program entry, not too many bells and whistles.

3.__name__ == __main__‘* * * *

When the.py file is run directly (Ctrl+Shift+F10), the code block under ** if __name__ == ‘__main__’ will be run, which is equivalent to Python’s main function entry **, as shown in the following example:

A. Create a new my_name. Py file as a module file:

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python __name__ == '__main__'. Py @time :2021/04/24 08:00 @motto: A thousand miles without a small stream without a river or sea. The wonderful program life needs to accumulate unremittingly! "" "# define a function and print the __name__ def prit_name () : print (" my_name. Py __name__ : ", __name__) if __name__ = = "__main__" : prit_name ()Copy the code

B. Create a python_main.py file and use it as the startup file (Ctrl+Shift+F10) :

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python __name__ == '__main__'. Py @time :2021/04/24 08:00 @motto: A thousand miles without a small stream without a river or sea. The wonderful program life needs to accumulate unremittingly! # define a function and print __name__ def prit_name(): my_name.prit_name() print("python_main.py __name__:", __name__) if __name__ == "__main__": Prit_name () "' output: my_name. Py __name__ : my_name python_main. Py __name__ : __main__ ' ' 'Copy the code

Python_main. py is the boot file whose built-in attribute __name** is equal to ‘__main__’, and myname. py is the import module whose __name** is equal to the filename (also known as the module name). So the expression in my_name. Py if __name__ == ‘__main__’ does not hold! arunachal

When my_name.py is directly used as the startup file (Ctrl+Shift+F10), output:

my_name.py __name__: __main__
Copy the code

conclusion

If the py file is imported as a module, __name__ is the filename of the py file (also known as the module name);

If the py file is run directly (Ctrl+Shift+F10), then __name__ defaults to the string ‘__main__’ **; arunachal

Role.

1.__name__ == ‘__main__’ as the main entry to start the py file;

2. A project will inevitably contain multiple module files, each module file after writing their own code will do some simple tests to detect bugs or write a simple example of their own function calls, and appropriately: __name__ == ‘__main__’ will not affect your test code, nor will it affect others to call your interface functions;

3.Guess you like

  1. The Python for loop
  2. The Python string
  3. The Python list
  4. The Python tuple tuple
  5. Python dictionary dict
  6. Python conditional derivations
  7. Python list derivations
  8. Python dictionary derivations
  9. Python function declarations and calls
  10. Python variable argument *argc/**kargcs
  11. Python anonymous function lambda
  12. Python return logic determines expressions
  13. Python string/list/tuple/dictionary conversions
  14. Python local and global variables
  15. The Python type function is different from the isinstance function
  16. Python is differs from ==
  17. Python mutable and immutable data types
  18. Shallow and deep copies of Python

Python __name__ == ‘__main__

This article is published by the blog – Ape Say Programming Ape Say programming!