如何总结Python functools模块的常用函数及其应用场景?

2026-04-30 20:061阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1269个文字,预计阅读时间需要6分钟。

如何总结Python functools模块的常用函数及其应用场景?

本文为家庭带来关于Python的相关知识,主要介绍了Python的functools模块的使用及说明,包含很好的参考价值,以下是一起看看:

Python functools模块

Python的functools模块提供了许多有用的函数,用于操作函数,包括但不限于:

1. 函数装饰器

2.高阶函数

3.函数包装器

4.函数生成器

以下是一些常用的functools函数:

如何总结Python functools模块的常用函数及其应用场景?

1. `functools.partial()`:将参数固定,返回一个新的函数。

2.`functools.update_wrapper()`:更新函数的wrapper。

3.`functools.wraps()`:装饰器,用于将wrapper的属性复制到被装饰的函数。

4.`functools.reduce()`:对序列进行迭代,应用一个函数,最终返回一个结果。

5.`functools.lru_cache()`:缓存函数的返回值,避免重复计算。

以下是一个使用`functools.partial()`的例子:

python

from functools import partial

def add(a, b): return a + b

add_five=partial(add, 5)print(add_five(3)) # 输出:8

希望本文对大家有所帮助!

本篇文章给大家带来了关于Python的相关知识,主要介绍了Python的functools模块使用及说明,具有很好的参考价值,下面一起来看一下,希望对大家有帮助。

程序员必备接口测试调试工具:立即使用
Apipost = Postman + Swagger + Mock + Jmeter
Api设计、调试、文档、自动化测试工具
后端、前端、测试,同时在线协作,内容实时同步

partial

用于创建一个偏函数,将默认参数包装一个可调用对象,返回结果也是可调用对象。

偏函数可以固定住原函数的部分参数,从而在调用时更简单。

from functools import partial int2 = partial(int, base=8) print(int2('123')) # 83登录后复制

update_wrapper

使用 partial 包装的函数是没有__name__和__doc__属性的。

update_wrapper 作用:将被包装函数的__name__等属性,拷贝到新的函数中去。

from functools import update_wrapper def wrap2(func): def inner(*args): return func(*args) return update_wrapper(inner, func) @wrap2 def demo(): print('hello world') print(demo.__name__) # demo登录后复制

wraps

warps 函数是为了在装饰器拷贝被装饰函数的__name__。

就是在update_wrapper上进行一个包装

from functools import wraps def wrap1(func): @wraps(func) # 去掉就会返回inner def inner(*args): print(func.__name__) return func(*args) return inner @wrap1 def demo(): print('hello world') print(demo.__name__) # demo登录后复制

reduce

在 Python2 中等同于内建函数 reduce

函数的作用是将一个序列归纳为一个输出

reduce(function, sequence, startValue) from functools import reduce l = range(1,50) print(reduce(lambda x,y:x+y, l)) # 1225登录后复制

cmp_to_key

在 list.sort 和 内建函数 sorted 中都有一个 key 参数

x = ['hello','worl','ni'] x.sort(key=len) print(x) # ['ni', 'worl', 'hello']登录后复制

Python3 之前还提供了cmp参数来比较两个元素

cmp_to_key 函数就是用来将老式的比较函数转化为 key 函数

lru_cache

允许我们将一个函数的返回值快速地缓存或取消缓存。

该装饰器用于缓存函数的调用结果,对于需要多次调用的函数,而且每次调用参数都相同,则可以用该装饰器缓存调用结果,从而加快程序运行。

该装饰器会将不同的调用结果缓存在内存中,因此需要注意内存占用问题。

from functools import lru_cache @lru_cache(maxsize=30) # maxsize参数告诉lru_cache缓存最近多少个返回值 def fib(n): if n < 2: return n return fib(n-1) + fib(n-2) print([fib(n) for n in range(10)]) fib.cache_clear() # 清空缓存登录后复制

singledispatch

单分发器, Python3.4新增,用于实现泛型函数。

根据单一参数的类型来判断调用哪个函数。

from functools import singledispatch @singledispatch def fun(text): print('String:' + text) @fun.register(int) def _(text): print(text) @fun.register(list) def _(text): for k, v in enumerate(text): print(k, v) @fun.register(float) @fun.register(tuple) def _(text): print('float, tuple') fun('i am is hubo') fun(123) fun(['a','b','c']) fun(1.23) print(fun.registry) # 所有的泛型函数 print(fun.registry[int]) # 获取int的泛型函数 # String:i am is hubo # 123 # 0 a # 1 b # 2 c # float, tuple # {<class 'object'>: <function fun at 0x106d10f28>, <class 'int'>: <function _ at 0x106f0b9d8>, <class 'list'>: <function _ at 0x106f0ba60>, <class 'tuple'>: <function _ at 0x106f0bb70>, <class 'float'>: <function _ at 0x106f0bb70>} # <function _ at 0x106f0b9d8>登录后复制

以上就是Python的functools模块使用总结的详细内容,更多请关注自由互联其它相关文章!

本文共计1269个文字,预计阅读时间需要6分钟。

如何总结Python functools模块的常用函数及其应用场景?

本文为家庭带来关于Python的相关知识,主要介绍了Python的functools模块的使用及说明,包含很好的参考价值,以下是一起看看:

Python functools模块

Python的functools模块提供了许多有用的函数,用于操作函数,包括但不限于:

1. 函数装饰器

2.高阶函数

3.函数包装器

4.函数生成器

以下是一些常用的functools函数:

如何总结Python functools模块的常用函数及其应用场景?

1. `functools.partial()`:将参数固定,返回一个新的函数。

2.`functools.update_wrapper()`:更新函数的wrapper。

3.`functools.wraps()`:装饰器,用于将wrapper的属性复制到被装饰的函数。

4.`functools.reduce()`:对序列进行迭代,应用一个函数,最终返回一个结果。

5.`functools.lru_cache()`:缓存函数的返回值,避免重复计算。

以下是一个使用`functools.partial()`的例子:

python

from functools import partial

def add(a, b): return a + b

add_five=partial(add, 5)print(add_five(3)) # 输出:8

希望本文对大家有所帮助!

本篇文章给大家带来了关于Python的相关知识,主要介绍了Python的functools模块使用及说明,具有很好的参考价值,下面一起来看一下,希望对大家有帮助。

程序员必备接口测试调试工具:立即使用
Apipost = Postman + Swagger + Mock + Jmeter
Api设计、调试、文档、自动化测试工具
后端、前端、测试,同时在线协作,内容实时同步

partial

用于创建一个偏函数,将默认参数包装一个可调用对象,返回结果也是可调用对象。

偏函数可以固定住原函数的部分参数,从而在调用时更简单。

from functools import partial int2 = partial(int, base=8) print(int2('123')) # 83登录后复制

update_wrapper

使用 partial 包装的函数是没有__name__和__doc__属性的。

update_wrapper 作用:将被包装函数的__name__等属性,拷贝到新的函数中去。

from functools import update_wrapper def wrap2(func): def inner(*args): return func(*args) return update_wrapper(inner, func) @wrap2 def demo(): print('hello world') print(demo.__name__) # demo登录后复制

wraps

warps 函数是为了在装饰器拷贝被装饰函数的__name__。

就是在update_wrapper上进行一个包装

from functools import wraps def wrap1(func): @wraps(func) # 去掉就会返回inner def inner(*args): print(func.__name__) return func(*args) return inner @wrap1 def demo(): print('hello world') print(demo.__name__) # demo登录后复制

reduce

在 Python2 中等同于内建函数 reduce

函数的作用是将一个序列归纳为一个输出

reduce(function, sequence, startValue) from functools import reduce l = range(1,50) print(reduce(lambda x,y:x+y, l)) # 1225登录后复制

cmp_to_key

在 list.sort 和 内建函数 sorted 中都有一个 key 参数

x = ['hello','worl','ni'] x.sort(key=len) print(x) # ['ni', 'worl', 'hello']登录后复制

Python3 之前还提供了cmp参数来比较两个元素

cmp_to_key 函数就是用来将老式的比较函数转化为 key 函数

lru_cache

允许我们将一个函数的返回值快速地缓存或取消缓存。

该装饰器用于缓存函数的调用结果,对于需要多次调用的函数,而且每次调用参数都相同,则可以用该装饰器缓存调用结果,从而加快程序运行。

该装饰器会将不同的调用结果缓存在内存中,因此需要注意内存占用问题。

from functools import lru_cache @lru_cache(maxsize=30) # maxsize参数告诉lru_cache缓存最近多少个返回值 def fib(n): if n < 2: return n return fib(n-1) + fib(n-2) print([fib(n) for n in range(10)]) fib.cache_clear() # 清空缓存登录后复制

singledispatch

单分发器, Python3.4新增,用于实现泛型函数。

根据单一参数的类型来判断调用哪个函数。

from functools import singledispatch @singledispatch def fun(text): print('String:' + text) @fun.register(int) def _(text): print(text) @fun.register(list) def _(text): for k, v in enumerate(text): print(k, v) @fun.register(float) @fun.register(tuple) def _(text): print('float, tuple') fun('i am is hubo') fun(123) fun(['a','b','c']) fun(1.23) print(fun.registry) # 所有的泛型函数 print(fun.registry[int]) # 获取int的泛型函数 # String:i am is hubo # 123 # 0 a # 1 b # 2 c # float, tuple # {<class 'object'>: <function fun at 0x106d10f28>, <class 'int'>: <function _ at 0x106f0b9d8>, <class 'list'>: <function _ at 0x106f0ba60>, <class 'tuple'>: <function _ at 0x106f0bb70>, <class 'float'>: <function _ at 0x106f0bb70>} # <function _ at 0x106f0b9d8>登录后复制

以上就是Python的functools模块使用总结的详细内容,更多请关注自由互联其它相关文章!