As shown below:
#Abstract (function) # 1. callable Determine if an object can be called x = 1 def y(): return None callable(y) # y can be called callable(x) # x can't be called # 2, when the function has no return function will return None by default # 3. the string placed at the beginning of the function becomes the document string as follows: def square(x): 'my name is hexianmin' #This is a document string that will be stored as part of the function # return x*x # 4, 'pass value' and 'pass address' in functions slice lists to produce an equal but not identical copy (i.e., the two lists do not store the same address) # pass values: pass variables when calling a function eg: x = 1 change(x) # pass address: pass list when calling function (note here: tuples can't be changed and passed over) eg: x = list('pyhon') change(x) # Function parameters: 1, positional parameters 2, keyword parameters # 1. Positional parameters: The relationship between real and formal parameters is a 'one-to-one' relationship The position of the real parameter determines the value received by the formal parameter. # 2. Keyword parameter: the specified keyword to pass the value (or address) to the formal parameter, like a dictionary, key-value correspondence. # Note: 1, the two can not conflict 2, the keyword parameters and location parameters can be mixed together, prioritize the keyword parameters, the rest according to the location of a one-to-one correspondence # * / ** The beauty of this: the role of collecting and assigning parameters. # Collect parameters: * : collect redundant general objects (positional parameters, dictionaries will also be used as positional parameters) into tuple type ** : collect redundant keyword parameters into dictionary type # Assign parameters: * : assign parameters of tuple type to formal parameters ** : assign parameters of dictionary type to formal parameters # Collect parameters: def print_params_1(x, y, z=3, *pospar, **keypar): # Note that z=3 here is a default value for z that is used when the function is called without assigning a value to z, but once the function is called with a value assigned to z, z doesn't have to have a default value. print(x, y, z) print(pospar) #1, without an asterisk (*) is a tuple 2, with an asterisk (*) is to take each value in the tuple out of the print(keypar) # used in the function: 1, without an asterisk (**) is a dictionary (but can not take the value out) 2, with an asterisk (*) is to take the dictionary out of each key 3, with two asterisks will report errors print_params_1(1, 2, 4, 5, 6, 7, foo=1, bar=2) # Assignment parameters: def foo(x, y, z, m=0, n=0): print(x, y, z) print(m) print(n) return -1 def call_foo(*args, **kwds): # Collect parameters print('calling foo!') foo(*args, kwds) # Assign arguments Here if you use foo(*args, **kwds) **kwds will report an error x1 = 1 y1 = 2 z1 = 3 d = { 'm1': 4, 'n1': 5 } print(call_foo(x1, y1, z1, d1=1, d2=2)) # A dictionary is called as a positional parameter # # Scope: 1, global variables 2, local variables # Note: To use a global variable in a local function (the default variable in a local function is a local variable): 1. use it only once (and rename it) 2. use it after declaring it (after declaring it, it's a global variable) # 1. Used only once (and renamed): para = 1 def combine(para): print(para,globals()['para']) # globals()['para'] combine(2) # 2. Use after declaration (after declaration it is a global variable): xx = 2 def change_global(): global xx # After the declaration it's a global variable xx = xx +2 print(xx) change_global() # 3. vars(): the assignment is actually an invisible dictionary The use of the return is a dictionary x11 = 1 x22 = vars() print(x22['x11']) # 4. vars() globals() locals() all return a dictionary when used # Scope nesting def multi(fac): def multiFac(num): # The multiFac(num) function is called: closure return num * fac return multiFac dou = multi(2) # The returned dou is now a function (multiFac(num) function) dou(3) # This is equivalent to calling multiFac(3) # list(map(str,range(10))) and [str(i) for i in range(10)] are equivalent # filter(lambda x: , seq) #from functools import reduce reduce(lambda x,y: x+y, numbers) # map filter reduce
Addendum: python parameter passing problem (parameter passing out)
Variables, Objects and Type Relationships
python is a dynamically typed language and does not require pre-declaration of the type of the variable; the type and value of the variable are initialized at the moment of assignment. Further, types in python belong to objects, not variables.
Example:
a=2 b=[1,2]
respectively, means to assign an int object, 2, to a; and a list object, [1,2], to b.
That is, the initialization of a variable is not completed until a python object of a different type is assigned to that variable via an assignment number, which makes the variable represent an object of a certain type.
function (math.)
Immutable parameter passing
If you want to pass parameters, you have to declare the variable of the parameter before defining the function in python, otherwise you will be prompted with the message global name 'abun1' is not defined, of course, the declaration process of the variable can be implicitly carried out.
For example, if a=2 or a={}, the initialization of the type of the variable is done the moment a value is assigned to a, i.e., the declaration of the variable is done.
However, it is especially important to note that int, long, bool, float,tuple() and other objects in python are immutable, so you cannot pass variables that output these types when passing parameters.
Example:
def tmpF(a): a=10 nint=2 tmpF(nint) print(nint) #The result is still2
Because, the variable nint represents an integer object 2, and when the function tmpF() is called, since the integer object cannot be changed, then a new integer object 10 is created so that a points to it, and therefore the integer object represented by nint remains 2, unchanged.
Changeable parameter passing
If, when defining a function, you want to output certain processed variables using parameters, you must use objects that can be changed, such as lists, dicts, and so on.
Example:
def tmpF(a): (2) nx=[] tmpF(nx) print(nx) #nx=[2]
Since, list is a mutable type object, that list type object is modified when the function tmpF() is called, and the nx points to that object still.
So, a function can output its parameters, via a mutably typed object.
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.