如何查看 python 的 functools.partial 修饰的对象?-灵析社区

D_Y_大师

import functools # 原始函数 def multiply(x, y): return x * y # 使用 functools.partial 创建一个新的可调用对象 partial_function = functools.partial(multiply, y=2) # 查看 partial_function 的类型 print(type(partial_function)) 比如上面的代码,输出就是 但是我想知道这个 partial_function 到底修饰了哪个 func 或者 class,怎么知道呢?

阅读量:196

点赞量:11

问AI
用 partial_function.func "partial object" (https://link.segmentfault.com/?enc=Q5qk9UQlgGhnYvjRK23nqQ%3D%3D.ut%2B2qaMzFbupwWClRauhOiTpKqpGT8%2FoR%2Fn3GEEP20Fl3n4qHxdm9pJWFbm3wMJE1bDW2kzSTPobSNFpMKlEIXOX0PodQUT%2BDNCmoIIUFy4%3D) «partial Objectspartial 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.»