我想类型提示一个方法,该方法可能会接收对 A 或 A 的子类进行操作的 Callable:
def do(f: Callable[[A], None])
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
def do(f: Callable[[typing.Type[A]], None])
Run Code Online (Sandbox Code Playgroud)
完整的虚拟示例如下所示:
class A:
n = 1
class B(A):
n = 2
def something_a(x: A):
print(x.n)
def something_b(x: B):
print(x.n)
def do(f: Callable[[A], None]):
f(B())
do(something_a) # ok
do(something_b) # PyCharm type check warnning
Run Code Online (Sandbox Code Playgroud) 我不明白accumarray传递复数时的行为。我希望accumarray([1 1]', [1i 2i]')表现得像sum([1i 2i])(对于这种特殊情况)。
然而它返回0.0000 - 3.0000i而不是0.0000 + 3.0000i.