python3(八) function
# Python 常用内置函数 https://docs.python.org/3/library/functions.html#abs print(help(abs)) # Return the absolute value of the argument. 返回参数的绝对值。 print(abs(-20)) # 20 print(max(2, 3, 1, -5)) # 3 # 类型转换 print(int('123')) # 123 print(int(12.34)) # 12 print(float('12.34')) # 12.34 print(float(12.34)) # 12.34 print(str(1.23)) # '1.23' print(str(100)) # '100' print(bool(1)) # True print(bool('')) # False # 定义函数 # 定义一个函数要使用def语句,依次写出函数名、括号、括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回 def my_abs(x): if x >= 0: return x else: return -x print(my_abs(-10)) # 10 # 空函数 啥也不做 ,用pass def nop(): pass # 实际上pass可以用来作为占位符,比如现在还没想好怎么写函数的代码,就可以先放一个pass,让代码能运行起来 # isinstance 类型检查,如下示例只允许Int和float参数进入 def my_abs(x): if not isinstance(x, (int, float)): raise TypeError('bad operand type') if x >= 0: return x else: return -x # 返回多个值 import math def move(x, y, step, angle=0): nx = x + step * math.cos(angle) ny = y - step * math.sin(angle) return nx, ny print(move(100, 100, 60, math.pi / 6)) # 返回值是一个tuple (151.96152422706632, 70.0) # 没有return语句时,自动return None # ------------函数的参数 # 计算x平方 def power(x): return x * x # 现在想计算x立方、x四次方。。。。怎么办? def power(x, n): s = 1 while n > 0: n = n - 1 s = s * x return s print(power(5, 5)) # 3125 # 默认参数 调用power(5)时,相当于调用power(5, 2) def power(x, n=2): s = 1 while n > 0: n = n - 1 s = s * x return s print(power(5)) # 25 print(power(5, 1)) # 5 # 如何设置默认参数 # 当函数有多个参数时,把变化大的参数放前面,变化小的参数放后面。变化小的参数就可以作为默认参数。 # 最大的好处是能降低调用函数的难度 def enroll(name, gender, age=6, city='Beijing'): print('name:', name) print('gender:', gender) print('age:', age) print('city:', city) print(enroll('Sarah', 'F')) # name: Sarah # gender: F # age: 6 # city: Beijing enroll('Bob', 'M', 7) print(enroll('Adam', 'M', city='Tianjin')) # -------------------------------------------------| # 定义默认参数要牢记一点:默认参数必须指向不变对象! # -------------------------------------------------| def add_end(L=[]): L.append('END') return L print(add_end()) # ['END'] print(add_end()) # ['END', 'END'] # 可以用None这个不变对象来实现,无论调用多少次,都不会有问题 def add_end(L=None): if L is None: L = [] L.append('END') return L print(add_end()) # ['END'] print(add_end()) # ['END'] # 可变参数 # 1,如下需要提前定义list 或者 tuple传入 def calc(numbers): sum = 0 for n in numbers: sum = sum + n * n return sum calc([1, 2, 3]) calc((1, 3, 5, 7)) # 2,定义可变参数 * def calc(*numbers): sum = 0 for n in numbers: sum = sum + n * n return sum calc(1, 2) # 5 calc(0) # 0 def person(name, age, **kw): print('name:', name, ',age:', age, ',kw:', kw) person('Michael', 30) # name: Michael ,age: 30 ,kw: {} person('Bob', 35, city='Beijing') # name: Bob ,age: 35 ,kw: {'city': 'Beijing'} person('Adam', 45, gender='M', job='Engineer') # name: Adam ,age: 45 ,kw: {'gender': 'M', 'job': 'Engineer'} extra = {'city': 'Beijing', 'job': 'Engineer'} person('Jack', 24, **extra) # name: Jack age: 24 other: {'city': 'Beijing', 'job': 'Engineer'} # 查是否有city和job参数 def person(name, age, **kw): if 'city' in kw: # 有city参数 print('city') if 'job' in kw: # 有job参数 pass print('name:', name, 'age:', age, 'other:', kw) person('Jack', 24, city='甘肃') # 命名关键字,* 号后面的说明参数名称必须是city,job def person(name, age, *, city, job): print(name, age, city, job) person('Jack', 24, city='Beijing', job='Engineer') # person('Jack', 24, city1='Beijing', job1='Engineer') 报错TypeError: person() got an unexpected keyword argument 'city1' # 函数定义中已经有了一个可变参数,后面跟着的命名关键字参数就不再需要一个特殊分隔符*了: # 使用命名关键字参数时,要特别注意,如果没有可变参数,就必须加一个*作为特殊分隔符。如果缺少*,Python解释器将无法识别位置参数和命名关键字参数 # ------------------------------------------------------------------ # 参数组合Python中,参数定义的顺序必须是:必选参数、默认参数、可变参数、命名关键字参数和关键字参数。 def f1(a, b, c=0, *args, **kw): print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw) def f2(a, b, c=0, *, d, **kw): print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw) f1(1, 2) # a = 1 b = 2 c = 0 args = () kw = {} f1(1, 2, c=3) # a = 1 b = 2 c = 3 args = () kw = {} f1(1, 2, 3, 'a', 'b') # a = 1 b = 2 c = 3 args = ('a', 'b') kw = {} f1(1, 2, 3, 'a', 'b', x=99) # a = 1 b = 2 c = 3 args = ('a', 'b') kw = {'x': 99} f2(1, 2, d=99, ext=None) # a = 1 b = 2 c = 0 d = 99 kw = {'ext': None} args = (1, 2, 3, 4) kw = {'d': 99, 'x': '#'} f1(*args, **kw) # a = 1 b = 2 c = 3 args = (4,) kw = {'d': 99, 'x': '#'} args = (1, 2, 3) kw = {'d': 88, 'x': '#'} f2(*args, **kw) # a = 1 b = 2 c = 3 d = 88 kw = {'x': '#'} # 递归 def fact(n): if n == 1: return 1 return n * fact(n - 1) print(fact(1)) print(fact(5)) # 120 5*(4*(3*(2*(1)...) print(fact(100)) # 尾递归是指,在函数返回的时候,调用自身本身,并且,return语句不能包含表达式。这样,编译器或者解释器就可以把尾递归做优化,使递归本身无论调用多少次,都只占用一个栈帧,不会出现栈溢出的情况。 def fact_iter(num, product): if num == 1: return product return fact_iter(num - 1, num * product) print(fact_iter(5, 1)) # 120 5-1,5*1->4-1,4*5->3-1,3*20->2-1,2*60->120