把函数视作对象
- __doc__属性生成对象帮助文本
def factorial(n):
""" return n! """
return 1 if n < 2 else n * factorial(n-1)
factorial.__doc__
- 通过别的名称使用函数,把函数作为参数传递
fact = factorial
fact(5)
list(map(factorial, range(11)))
高阶函数
- sorted
fruits = ['strawberry', 'fig', 'apple', 'cherry', 'raspberry', 'banana']
# key参数可以为任何单参数函数
sorted(fruits, key=len)
# 反向拼写单词
def reverse(word):
return word[::-1]
sorted(fruits, key=reverse)
map、filter和reduce的替代品
# map和filter列表推导比较
list(map(fact, range(6)))
[fact(n) for n in range(6)]
# reduce和sum
from functools import reduce
from operator import add
reduce(add, range(100))
sum(range(100))
匿名函数
fruits = ['strawberry', 'fig', 'apple', 'cherry', 'raspberry', 'banana']
# 反转拼写并排序
sorted(fruits, key=lambda word: word[::-1]
可调用对象
- 调用类时会运行类的__new__方法创建一个实例,然后运行__init__方法,初始化实例,最后把实例返回给调用方。
- callable()判断对象能否调用
abs, str, 13
[callable(obj) for obj in (abs, str, 13)]
函数内省
- 函数得属性:dir(factorial)
# 常规对象没有而函数有得属性
class C: pass
obj = C()
def func(): pass
sorted(set(dir(func) - set(dir(obj)))
定位参数到仅限关键字参数
def tag(name, *content, cls=None, **attrs):
"""生成一个或多个HTML标签"""
if cls is not None:
attrs['class'] = cls
if attrs:
attr_str = ''.join(' %s="%s"' % (attr, value) for attr, value in sorted(attrs.items()))
else:
attr_str = ''
if content:
return '\n'.join('<%s%s>%s</%s>' % (name, attr_str, c, name) for c in content)
else:
return '<%s%s />' % (name, attr_str)
# 关键字参数cls,必须传'cls=',由于使用了关键字后,后面得都要关键字传参,所以*content不定惨必须要在关键字参数前
print(tag('p', 'hello', 'world', cls='sidebar'))
# 字典传参
my_tag = {'name': 'img', 'title': 'Sunset Boulevard','src': 'sunset.jpg', 'cls': 'framed'}
tag(**my_tag)
获取关于参数的信息
- 函数对象__defaults__属性,保存着定位参数和关键参数得默认值
def clip(text, max_len=80):
"""在max_len前面或后面的第一个空格处截断文本"""
end =None
if len(text) > max_len:
space_before = text.rfind(' ', 0, max_len)
if space_before >= 0:
end = space_before
else:
space_after = text.rfind(' ', max_len)
if space_after >= 0:
end = space_after
if end is None:
end = len(text)
return text[:end].rstrip()
clip.__defaults__
clip.__code__
# 函数所有的局部变量名
clip.__code__.co_varnames
# 参数数量
clip.__code__.co_argcount
- inspect提取参数
from inspect import signature
sig = signature(clip)
str(sig)
for name, param in sig.parameters.items():
print(param.kind, ':', name, '=', param.default)
函数注解
- Python不做检查、不做强制、不做验证,什么操作都不做。只是把注解存储在函数的__ammotations__属性里。
def clip(text:str, max_len:'int > 0'=80) -> str:
"""在max_len前面或后面的第一个空格处截断文本"""
end =None
if len(text) > max_len:
space_before = text.rfind(' ', 0, max_len)
if space_before >= 0:
end = space_before
else:
space_after = text.rfind(' ', max_len)
if space_after >= 0:
end = space_after
if end is None:
end = len(text)
return text[:end].rstrip()
clip.__annotations__
operator和functools包
- reduce函数
from functools import reduce
# 匿名函数求阶乘
def fact(n):
return reduce(lambda a, b: a*b, range(1, n+1))
# operator.mul函数求阶乘
from operator import mul
def fact(n):
return reduce(mul, range(1, n+1))
- itemgetter排序
metro_data = [('Tokyo', 'JP', 36.933, (35.689722, 139.691667)),('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),('Mexico City', 'MX', 20.142, (19.433333, -99.133333))]
from operator import itemgetter
# itemgetter(1)创建一个接受集合的函数,返回索引位 1 上的元素
for city in sorted(metro_data, key=itemgetter(1)):
print(city)
# 多个参数时,返回提取的索引元组
cc_name = itemgetter(1, 0)
for city in metro_data:
print(cc_name(city))
- functools.partial冻结参数:把原函数的某些参数固定
from operator import mul
from functools import partial
triple = partial(mul, 3)
triple(7)
# 冻结后相当于单参数函数
list(map(triple, range(1, 10)))
流畅的python2015