文档字符串:在函数中写文档
def square(x):
"document strings"
return x * x
可以如下显示
print(square.__doc__)
help(square)
example:
def func(posParam, keyParam=defaultValue, *posColParam, **keyColParam):
print(posParam)
print(keyParam)
print(posColParam) # as tuple
print(keyColParam) # as dict
example:
def print_params(x, y, z = 3, *pospar, **keypar):
print(x, y, z)
print(pospar)
print(keypar)
print_params(1,2,3, 5,6,7, foo=1,bar=2)
屏蔽(shadowing)
对于同名变量,局部变量能屏蔽(shadow)全局变量
global
mint:按照理解应该是独立的内存空间或者stack吧
闭包(closure):函数存储于封闭作用域的行为
def Multiplier(factor):
def MultiplierByFactor(number):
return number * factor
return MultiplierByFactor
递归的定义:引用自身
recursion
: please see the definitionrecursion
注意避免写出没有边界条件的infinite recursion
否则会产生“超过最大递归深度”的错误
一般函数