Python中的两个等号==如何工作?

class A:
    def __init__(self, key: str):
        self.key = key

    def __str__(self):
        return self.key

    def __eq__(self, rhs):
        print('A')
        return self.key == str(rhs)


class B:
    def __init__(self, key: str):
        self.key = key

    def __str__(self):
        return self.key

    def __eq__(self, rhs):
        print('B')
        return self.key == str(rhs)


print(A('a') == B('a'))
print('a' == B('a'))
print(A('a') == B('a') == A('a') == B('a'))
A
True
B
True
A
B
A
True

猜你喜欢

转载自blog.csdn.net/dscn15848078969/article/details/121479538
今日推荐