1. The default parameters of the function must be immutable
If the default parameter of a function is a mutable object, then the default parameter is modified outside the function and affects the function itself.
def spam (A, b=None): # B to be an immutable parameter, you cannot use a variable parameter such as an empty list [] ... if is None: ... = []...
2. Anonymous functions
1. You can use anonymous functions when you can't think of a function name or want a short operation
Lambda x, y:x + y>>> sum (2, 3)def sum (x, y): # the anonymous function above, Equivalent to this function ... Print (x + y) ... >>> sum (3, 4)7
2. Binding variables in anonymous functions
Lambda y:x + yLambda y:x +y>>> A (5) # found not expected 15 because X was changed 25>>> B (5)Lambda y, x=x:x + y # bind x,x as a local variable when defined, not affected by x change Lambda y, x=x:x + y>>> A (5)30>>> B (5)20
Fluent Python and Cookbook learning Notes (eight)