__abs(self)__?

Mike Carifio carifio.nospam at nospam.usys.com
Fri Mar 1 13:49:47 EST 2002


I'm reading about special method names. __abs__(self), which maps to the
abs() method seems unneccessary(?).
So:

class ExampleNumber:
    def __init__(self, initializer):
        self.value = initializer
    def __abs__(self):
        return abs(self.value)

>>> x = ExampleNumber(-1)
>>> x.abs()
1

seems no different from:

class ExampleNumber:
    def __init__(self, initializer):
        self.value = initializer
    def abs(self):   # name isn't special?
        return abs(self.value)

abs is a builtin function, so why do I need a special name for a function?
I'm missing something...





More information about the Python-list mailing list