小编Hel*_*ann的帖子

Point() 接受 0 个位置子模式(给定 2 个)

我试图运行文档中的示例,但出现错误:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: Point() accepts 0 positional sub-patterns (2 given)
Run Code Online (Sandbox Code Playgroud)

有人可以解释我在这里做错了什么吗?

class Point():
    def __init__(self, x, y):
            self.x = x
            self.y = y

x, y = 5 ,5
point = Point(x, y)
match point:
    case Point(x, y) if x == y:
        print(f"Y=X at {x}")
    case Point(x, y):
        print(f"Not on the diagonal")
Run Code Online (Sandbox Code Playgroud)

python python-3.10 structural-pattern-matching

7
推荐指数
1
解决办法
2064
查看次数