Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

Testing for reference equality in Python

Say I have a class in Python that has an eq method defined for comparing attributes for equality:

class Foo(object):
    # init code...

    def __eq__(self, other):
        # usual eq code here....

How can I then compare two instances of Foo for reference equality (that is test if they are the same instance)? If I do:

f1 = Foo()
f2 = Foo()
print f1 == f2

I get True even though they are different objects.

question from:https://stackoverflow.com/questions/7274097/testing-for-reference-equality-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Thats the is operator

print f1 is f2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...