Describes the use of a basic function and
Why use functions?
- Avoid code reuse
- Improve code readability
Definition and calling functions
def function name (parameter 1, parameter 2)
'' 'function annotation' ''
Print ( 'function body')
return Return value
Definition: beginning def keyword, followed by a space followed by the function name and parentheses, finally, add a colon.
Function name: function name that contains any combination of letters, numbers, underscores, but can not start with a number. Although the function name can be easily named, but generally try to be defined to represent the function does.
Function's return value
The return action: a function execution ends
first returned value can be any data type.
Function can return values: If there is a return value, you must use a variable reception will be effective
No return value when three cases:
- When not writing return, the function returns a value None
- When a write-only return when the function returns a value None
- return None when the function returns a value None (almost no)
returns a return value (a variable)
return to return multiple values (a plurality of variables): a plurality of values separated by commas, returned as a tuple.
Receiving: receiving a variable can also be received by a plurality of variables, it returns several variables to receive a few
Function parameters
DEF Fun (S): # parameter accepts: formal parameters, referred to as parameter '' ' computing performance function --------- function of string length parameter s: string to accept the calculated ---- ---- information parameter return: return to calculate the length of the string --------- information worthy '' ' length = 0 for I in S: length + =. 1 return length RET = Fun ( ' Helloword ' ) # argument to: actual parameters, arguments referred Print (RET) arguments and parameters parameter: the definition of the time parameter is a function defined arguments: time passed in the function call parameter passing plurality of parameters can pass multiple parameter, multiple parameters are separated by commas. Standing on the angle parameter passing, function call parameters passed in two ways: according to a position-parameters Keyword parameters passed according to the dynamic parameters by receiving args unified by location value-passing extra parameters, stored in the form of a tuple by receiving a plurality of key values of key parameters transmitted and received by kwargs, stored in the form of a typical args : DEF Fun (a, B, * args): SUM = a + B for I in args: SUM + = I return SUM Print (Fun (1,5,6,4)) # output 1 + 4 + 5 + 6 and kwargs: DEF Fun (A, B, ** kwargs): Print (A, B, kwargs) # follow the keyword-parameters Fun (A = 10, B = 20 is, CCCC = 30, dddd = 50) #Output 20 is {10 'CCCC': 30, 'dddd': 50} DEF F (A, B, * args, the defult =. 6, ** kwargs): # position parameter, * args, default parameters, kwargs ** # Print (a, b, args, defult, kwargs) return a, b, args, defult, kwargs # pass parameters when: You must pass parameters by position, and then pass parameters by keyword Print (f (, 2, 7, 8, ccc = 10, der = 5))
Two nested functions and scope chain
1 function namespace
Global namespace: space "relationship variable name and the value of" Creating a store called the global namespace
local namespace: temporary space opened up in the run function in called the local namespace
built namespace: built-in namespace stored for python interpreter provides us with the names: input, print, str, list , tuple ... they are all familiar to us, you can use the method of take over.
Loading sequence between the three namespaces and values in this order:
Load order: Built-in (before running load) -> Global (load order from top to bottom coming) -> local (when the call load) ---> Built
values: local calls: local namespace - -> global namespace ---> built namespace
2 Scope: is the scope
Namespaces and scopes are inseparable
Scope divided into two types:
Global scope: global name space and the built-in namespace names are part of the global scope can be referenced anywhere in the entire file, globally effective
local scope: local namespace, it can only take effect in the local area
From the overall look:
- Using the name of the time: If there is global, with global
- If there is no global, with built-in
- globals: Check the name of the global scope of the [print (globals ())]
- locals: Check the name of the local scope [print (locals ())]
Chong inside the function
global name name = 'hahha'
Closure 3
Closed: internal function
package: contains external references to variables function scope
defined closure: if in a function of the external scope (but not global scope) a variable reference function was inside considered closure
closure = function defined environment when the function block (internal functions) +
DEF Outer (): X = 10 DEF Inner (): # Condition 1 inner is an internal function X = 20 is Print (X) # a variable condition of the second external environment return Inner # Outer () () F = Outer () # Returns It is the memory address of inner Print (f) f ()