Python study notes - module

The following source code is stored in hello.py

# ! / Usr / bin / env python3 # tell Unix / Linux / Mac file as a Python executable file 
# - * - Coding: UTF-8 - * - # This file uses the standard UTF-8 encoding format

' A the Test Module '  # documentation comments, the first string assignment module code are treated as comments on the current document module

__author__ = ' Michael Liao '  # __author__ variables are stored Author

Import sys # sys sys module variables can access the full functionality

def test():
    args = the sys.argv # the argv parameter sys, the list of all parameters stored in the command line, argv at least one element, i.e., the file name of the current file py (the argv [0]) 
    IF len (args). 1 == :
         Print ( ' ! the Hello, World ' )
     elif len (args) == 2 :
         Print ( ' the Hello,% S! ' % args [. 1 ])
     the else :
         Print ( ' Too MANY arguments! ' )
# Save the file as the current file hello.py, when the command line module files are run hello, Python interpreter to set a special variable __name__ __main__, and if the hello module introduced elsewhere, 
#if check will fail Therefore, if the test can make a module to perform some additional code command line to run through, this is the most common test run.
IF the __name__ == ' __main__ ' :
  Test ()

 

Run command line:

[@wooluwakerdeMBP:test]$ python hello.py Michael

Hello, Michael!

 

Python interactive environment, introducing hello module:

When import a module, the module will not be executed; only manually call its methods to trigger execution;

[@ WooluwakerdeMBP : the Test ] $ Python into the Python interactive environment ###

Python 3.7.5 (default, Oct 25 2019, 10:52:18) 

[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin

Type "help", "copyright", "credits" or "license" for more information.

 

>>> import hello

>>> hello.test()

Hello, world!

 

Scope

  • Normal function and variable names are public (public), it may be directly referenced, such as: abc, x123, PIand the like;
  • Similar _xxxand __xxxsuch function or variable is non-public (Private), it should not be directly referenced, for example _abc, __abcand the like;
  • Only external function needs to be called only defined as public, need not be defined as a function of external calls private, may be encapsulated in the public process private methods

Guess you like

Origin www.cnblogs.com/wooluwalker/p/12242471.html