小编Mar*_*man的帖子

支持子类的 Callable 的类型提示?

我想类型提示一个方法,该方法可能会接收对 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)

python python-typing

5
推荐指数
1
解决办法
883
查看次数

具有复数的 matlab accumarray

我不明白accumarray传递复数时的行为。我希望accumarray([1 1]', [1i 2i]')表现得像sum([1i 2i])(对于这种特殊情况)。

然而它返回0.0000 - 3.0000i而不是0.0000 + 3.0000i.

matlab complex-numbers

2
推荐指数
1
解决办法
48
查看次数

标签 统计

complex-numbers ×1

matlab ×1

python ×1

python-typing ×1