Python functions,
1. Create a function
Start with the 'def 'keyword, followed by the function name, and a parameter in parentheses, followed by a colon, return, and None if ruturn is not written.
Def Greet (name): print ("Hello" + name) Greet ("Python") Output: Hello Python
1.1 Important Function Attributes
Def Greet (name): "Greet Description: docized function" print ("Hello" + name + "\ n") Greet ("Python") print (Greet. _ doc _) # returns the document string print (Greet. _ name _) # returns the function name print (Greet. _ module _) # Return the name of the module where the function definition is located. Output: Hello PythonGreet Description: docized function Greet _ main __
1.2 parameter 1.2.1 keyword parameters and default values
# Using Keyword parameters can ignore Parameter order # location parameters can be used together with keyword parameters, but location parameters must be placed before the keyword parameters. Def hello (grespon, name, pun = "! "): Print (" % s, % s "% (greased, name, pun) hello (" Hello "," Bob ") hello (name = "world", grespon= "Hello") # keyword hello ("Hello", "Bob", pun = ". ") Output: Hello, Bob! Hello, world! Hello, Bob.
1.2.2 collection Parameters
Def func3 (title, * params): print (title) print (params) func3 ("title", "Testing", "Testing2", "Testing3") output: title ('testing', 'testing2', 'testing3') # You can see that the asterisks before the parameter put all values in the same tuples. The asterisks mean to collect parameters from other locations, if not provided, it is an empty tuples. # "**" Processing keyword parameters return dictionary def print_params (** params): print (params) print_params (x = 1, y = 2, z = 3) output: {'X': 1, 'z': 3, 'y': 2} example of joint use: # "**" processing keyword parameter def print_params (param1, param2, param3 = 7, * params4, ** params5): print (param1, param2, param3) print (params4) print (params5) print_params ("pa1", "pa2 ", "pa3", "pa4", "pa5", "F6", name = "Bob", age = 18, sex = "male") output: pa1 pa2 pa3 ('pa4', 'pa5', 'pa6') {'sex': 'male', 'name': 'bob', 'age': 18}
1.2.3 inverse process of collecting Parameters
Def add (x, y): return x + yparams = (1, 2) print (add (* params) Output 3def printUserInfo (** params): print (params) params2 = {"name": "bob", "age": 20, "sex": "male"} printUserInfo (** params2) Output: {'sex ': 'male', 'name': 'bob', 'age': 20}
2. Scope
X = 1 # global variable def func (): # each function call will create a new scope x = 3 func () # It will not affect global variables, there is no problem when using the global variable name as the parameter name print (x) Output: 1
If the name of a local variable or parameter is the same as that of the global variable to be accessed, the global variable is blocked by the local variable.
Par = "thon" def combie (par): print (par + globals () ["par"]) combie ("py") Output: python
Rebind global variables
X = 1def change (): global x = x + 1 change () print (x) Output: 2
3. Miscellaneous
Nested scopes. Python functions can be nested.
def foo(): def bar(): print("Hello") bar()
Closure
# The closure contains free variables (not bound to a specific object). These variables are not defined in this Code block or in any global context, instead, it is defined in the environment where the code block is defined (local variables ). The word "closure" comes from the combination of the two: the code block to be executed (because free variables are contained in the Code block, these free variables and the objects they reference are not released) and provides a bound Computing Environment (SCOPE) for free variables ). (Reference Baidu encyclopedia) def foo (fac): def bar (num): return num * fac return bardouble = foo (2) print (double (3 )) tri = foo (5) print (tri (6) print (foo (7) (8) Output: 63056
Recursion
# Factorial def factorial (n): if n = 1: return 1 else: return n * factorial (n-1) print (factorial (5) output: 120 # power def power (x, n): if n = 0: return 1 else: return x * power (x, n-1) print (power (2, 3) Output: 8
Lambda expressions (anonymous functions)
# Python only supports anonymous functions in some simple cases. Sum = lambda x, y: x + y "equivalent to: def f (x, y): return x + y" print (sum (2, 3 )) output 5