コード例 #1
0
def test_inheritance_and_multiple_dispatch():
    @dispatch
    def f(x: A, y: A):
        return type(x), type(y)

    @dispatch
    def f(x: A, y: B):
        return 0

    assert f(A(), A()) == (A, A)
    assert f(A(), C()) == (A, C)
    assert f(A(), B()) == 0
    assert f(C(), B()) == 0
    assert raises(NotImplementedError, lambda: f(B(), B()))
コード例 #2
0
def test_inheritance_and_multiple_dispatch():
    @dispatch
    def f(x: A, y: A):
        return type(x), type(y)

    @dispatch
    def f(x: A, y: B):
        return 0

    assert f(A(), A()) == (A, A)
    assert f(A(), C()) == (A, C)
    assert f(A(), B()) == 0
    assert f(C(), B()) == 0
    assert raises(NotImplementedError, lambda: f(B(), B()))
コード例 #3
0
def test_singledispatch():
    @dispatch
    def f(x: int):
        return x + 1

    @dispatch
    def g(x: int):
        return x + 2

    @dispatch
    def f(x: float):
        return x - 1

    assert f(1) == 2
    assert g(1) == 3
    assert f(1.0) == 0

    assert raises(NotImplementedError, lambda: f('hello'))
コード例 #4
0
def test_singledispatch():
    @dispatch
    def f(x: int):
        return x + 1

    @dispatch
    def g(x: int):
        return x + 2

    @dispatch
    def f(x: float):
        return x - 1

    assert f(1) == 2
    assert g(1) == 3
    assert f(1.0) == 0

    assert raises(NotImplementedError, lambda: f('hello'))