(I'm sure this is a FAQ, but also hard to google)
Why does Python use abs(x) instead of x.abs?
As far as I see everything abs() does besides calling x.__abs__
could just as well be implemented in object.abs()
Is it historical, because there hasn't always been a root class?
The official answer from Guido van Rossum, with additional explanation from Fredrik Lundh, is here: http://effbot.org/pyfaq/why-does-python-use-methods-for-some-functionality-e-g-list-index-but-functions-for-other-e-g-len-list.htm
In a nutshell:
abs(x) reads more naturally than x.abs() for most such operations
you know that abs(x) is getting an absolute value, whereas a method x.abs() could mean something different depending on the class of x.
I think you are looking a typical example where a language designer decides that readability and terseness trump purist constructs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With