EDIT: Well, I'm trying to repeat this (English is not my native language) - with an example.
Some functions — I call them fun(object)
use object.__func__()
to do the job, or use other functions in object
if there is no object.__func__()
-
For example str()
- when using str(object)
it calls object.__str__()
, but if there is no object.__str__()
, it calls object.__repr__()
.
So you can use str()
with an object that does not have __str__()
and will still get some result.
-
Another example <
- when using a < b
he tries to use a.__lt__()
, but if there is no a.__lt__()
, he tries to use a.__gt__()
(and possibly other functions too)
class MyClass(): def __str__(self): return "MyClass: __str__" def __repr__(self): return "MyClass: __repl__"
You can remove __str__
to get a different result.
You can change True/False
to __lt__
, and you can see that this result has changed.
Then you can remove __lt__
, and you can change True/False
to __gt__
, and you will see that the result has changed again.
source share