As I understand, one can call a magic method of an object in either of two ways. Firstly like this, where the magic method is called like any regular method:
x = (1, 2, 3, 4)
print(x.__len__())
#prints 4
And secondly, like this, where the magic method is called "specially":
x = (1, 2, 3, 4)
print((len(x)))
#prints 4
As we can see, both are equivalent. However, when I call the magic method reversed()
upon a tuple with the first way, I am met with an error:
x = (1, 2, 3, 4)
x.__reversed__()
#causes an attribute error
Yet the "special" way of calling the reversed method works perfectly fine:
x = (1, 2, 3, 4)
reversed(x)
#returns a reversed object
Why does this occur? Have I misunderstood something about dunder methods?