本文共 1023 字,大约阅读时间需要 3 分钟。
callable判断 一个东西 是否可以调用。感觉没啥用!!!
>>> x=1>>> import math>>> y=math.sqrt>>> callable(x)False>>> callable(y)True__doc__
>>> def square(x): 'calculate the square of the number x' return x*x>>> square.__doc__'calculate the square of the number x'>>>列表传参本质上是指针:
>>> def change(n): n[0]="asdf" >>> name=['qqq','bbb']>>> change(name)>>> name['asdf', 'bbb']>>>全局变量:
>>> x=1>>> def change_global(): global x x+=1 >>> change_global()>>> x2bisect模块实现二元查找。
map:
>>> map(str, range(10))filter:
>>> seq=['foo', 'x41', '?!', "***"]>>> def func(x): return x.isalnum()>>> filter(func, seq)>>> list(filter(func, seq))['foo', 'x41']>>>
>>> [x for x in seq if x.isalnum()]['foo', 'x41']lambda
>>> filter(lambda x: x.isalnum(), seq)reduce:前两个元素与给定的函数联合使用,返回值与第三个元素继续联合使用。>>> list(filter(lambda x: x.isalnum(), seq))['foo', 'x41']
>>> from functools import reduce>>> num=[72, 101, 108, 111, 44, 53]>>> reduce(lambda x,y : x+y, num)489>>>
转载地址:http://nqmmi.baihongyu.com/