如何查看 python 的 functools.partial 修饰的对象?
import functools
# 原始函数
def multiply(x, y):
return x * y
# 使用 functools.partial 创建一个新的可调用对象
partial_function = functools.partial(multiply, y=2)
# 查看 partial_function 的类型
print(type(partial_function))
比如上面的代码,输出就是
<class 'functools.partial'>
但是我想知道这个 partial_function 到底修饰了哪个 func 或者 class,怎么知道呢?
回复
1个回答
test
2024-06-26
用 partial_function.func
partial Objects
partial objects are callable objects created by partial(). They have three read-only attributes:
partial.func
A callable object or function. Calls to the partial object will be forwarded to func with new arguments and keywords.partial.args
The leftmost positional arguments that will be prepended to the positional arguments provided to a partial object call.partial.keywords
The keyword arguments that will be supplied when the partial object is called.partial objects are like function objects in that they are callable, weak referencable, and can have attributes. There are some important differences. For instance, the
__name__
and__doc__
attributes are not created automatically. Also, partial objects defined in classes behave like static methods and do not transform into bound methods during instance attribute look-up.
回复
适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容