我想使用 Python3 partialmethod并且我无法调用结果函数。我的代码基于 Python 文档https://docs.python.org/release/3.6.4/library/functools.html 中的示例
from functools import partialmethod
class AClass():
def __init__(self):
self.v = val
def _fun(self, x):
z = x + self.v # some computation
return z
def fun(self, x):
return partialmethod(self._fun, x)
a = AClass(10)
b = a.fun(1)
print(b()) # TypeError: 'partialmethod' object is not callable
print(a.b()) # AttributeError: 'AClass' object has no attribute 'b'
Run Code Online (Sandbox Code Playgroud)
我明白为什么a.b()不正确,因为 b 没有在实例化的 a 中定义。的错误消息b()没有给我足够的信息来了解发生了什么。
如何定义带有绑定参数的方法并从封闭类外部调用它?这是可能的,还是有更好的方法来实现这个结果?
在 Python 中,内置常量 None 保证是单数。这种情况只有一个例子。
其他内置类型的情况并非如此。具有相同内容的字符串或数字并不唯一。两个字符串“a”和“a”不是唯一的,而是具有不同的实例。对于具有相同内部值的两个数字(例如 1.0 和 1.0)也是如此。
内置布尔常量 True 和 False 是否像 None 一样唯一,或者它们是否有不同的实例?
一个具体的例子是:
value = 1 == 1
Run Code Online (Sandbox Code Playgroud)
在这种情况下与?value相同的实例True