0

Does python internally overload the "==" when I implement the __cmp__ function in my class, just how we do that in C++ ?

Just curious. I am new to python. :)

1

4 Answers 4

3

The meaning of == as used in your Python code changes when you define __cmp__. In this particular sense, the "==-operator" on the Python level is modified by your definition of __cmp__ (note that this is only true if you don't also define __eq__).

But the operator== on the C++ level is not affected by this, for two reasons:

  • Python isn't implemented in C++, but in C, and there is no operator overloading
  • Python itself isn't recompiled when you write or use your Python code
Sign up to request clarification or add additional context in comments.

2 Comments

By python level is it meant that It will be changed globally ?
@DeepankarBajpeyi No, only in your code (and anything that imports it).
1

Yes, along with __eq__, __ne__ __lt__, __le__, __gt__ and __ge__(doc)

Comments

1

As opposed to what? That's what __cmp__ is for.

Though it's been deprecated for a while, and it's gone in 3 entirely, and you should use __eq__ and friends instead.

Comments

0

Yes, and likewise with a lot of other operators and methods. There's a full listing in the Python docs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.