The basic syntax of the python function <three>

Arguments and parameters:
  Defined Function brackets commonly called the formal parameter
  argument when calling brackets commonly called the arguments passed

Students. DEF (Age): 
    Print ( '% S My Age IS' Age%) 
Students. (18) 
Age is the parameter, the argument is 18

Specific application parameters:

1, the position parameter: from left to right in the order defined by the parameter
  location parameter: Parameter Mandatory
  position argument: the position according to the transmission parameter value

foo DEF (X, Y): 
    Print (X, Y) 
foo (1,2) 
# Results: 1,2  

2, keyword arguments:

  Argument defined in the form of a key = value
  necessarily to pass parameter values for the positions

Attention to the problem:
  1. Keyword argument must right argument position
  2 can not be repeated on the same transmission parameter values

def foo(x,y):
    print(x,y)
foo(y=2,x=1)
结果:1,2  

3, the default parameters:

  When the parameter definition has been assigned values
  may be passed by value can not be passed by value, often need to define the parameters becomes a position parameter, a smaller change parameter defined default parameters (parameters)
problems noted:
  1. Only when defining a assignment
  2. default parameters should be defined at a position right shape parameters
  3. default parameters should generally be defined as immutable type

foo DEF (X, Y = 2): 
    Print (X, Y) 
foo (. 1) 
# Results: 1,2 
foo (1,3) 
# Results: 1,3  

4, the variable-length parameters:
  a variable length refers to the number of arguments value is not fixed
  and there are arguments defined by location and by keywords in two forms, for both forms of variable length, has a corresponding parameter Two solutions to store them completely, and are * argsh ** kwargs

# * args: parameter transfer becomes tuple 
DEF foo (X, Y, * args): 
    Print (X, Y, args) 
foo (l, 2,3) 
# Results: 1 2 (3) 
# * * kwargs: parameter transfer dictionary becomes DEF foo (X, Y, kwargs **): Print (X, Y, kwargs) foo (1,2,. 3 a =, B =. 4) # results: 1 2 { 'a': 3, 'b  ': 4}

return statement:

  return statement [Expression] to exit the function returns a selective expression to the caller.

  return statement with no parameter values ​​return None. No previous example demonstrates how to return a value, the following example will show you how to do it:

# Writable Function Description 
DEF add_sum (arg1, arg2): 
    # Returns the two parameters and. " 
    Total = arg1 + arg2 
    Print (" the function: ", Total) 
    return Total 
# call the sum function of 
total = add_sum (10, 20 )

Namespace:
  1. built-in namespace: such as print, max ... (ctrl + Left view source) of these built-in functions that exist place called built-in namespace
  2. The global name space: the top grid to write belongs to the global namespace inside
  3. the local name space: the code function inside the local name space belonging to

Scope: namespace access order
  summary: the loading order of the three
  built - -> Global - -> Local

Access order three
  local - -> Global - -> Internal

def f1():
    def f2():
        def f3():
            print(print)
        f3()
    f2()
f1() 

Guess you like

Origin www.cnblogs.com/twoo/p/11750321.html