The __abs__() method in Python specifies what happens when you call the built-in abs() function on an object.
For example, let’s call abs() on a negative number:
n = -10 print(abs(n))
The result is the absolute value of the negative number, that is, the distance from 0.
The important part here is to understand that calling abs(n) is the same as calling n.__abs__():
n = -10 print(abs(n)) print(n.__abs__())
In this guide, you will learn what is the __abs__() method and how to customize it for your class.
Double Underscore Methods in Python
If you don’t know what is the meaning of the double underscore in the method, here is a quick primer.
In Python, double-underscore methods (dunder methods) are used for operator overloading.
These dunder methods are implemented in classes to specify what happens when a certain operation is performed on an object.
A common example of a dunder method is the __init__ method. This method gets called when an object is created.
class Fruit: def __init__(self, name): self.name = name # This calls __init__ with "Banana" argument: banana = Fruit("Banana") print(banana.name) # prints "Banana"
Another common dunder method is the __add__ method that specifies what happens when you add two objects with the + operator.
class Fruit: def __init__(self, name): self.name = name def __add__(self, otherfruit): # Creates a new Fruit with the two names combined return Fruit(self.name + otherfruit.name) banana = Fruit("Banana") apple = Fruit("Apple") combo = banana + apple print(combo.name) # Prints "BananaApple"
One thing you need to know about dunder methods is you can call them directly too.
For example, in the above example, instead of:
combo = banana + apple
You could have called:
combo = banana.__add__(apple)
The __abs__() Method in Python
In Python, __abs__() method is a special method that describes the absolute value of the object.
You can implement this object in your custom class to define what happens when someone calls abs() on your objects.
When talking about numbers, the absolute value measures the distance from 0. In layman’s terms, an absolute value removes the negative sign from a negative number.
Thus, in Python, the int type implements the __abs__() method such that when you call abs() on an integer, you get its absolute value.
n = -10 print(abs(n))
(You could also call n.__abs__() instead of abs(n).)
Customize __abs__() Method in Your Class
Meanwhile, I cannot imagine a practical use case for implementing the __abs__() method, but you should still know it is possible.
This means you implement the __abs__() method in your class to specify what happens when you call abs() on your objects.
For example, let’s implement a new class, numstr, that represents integers as strings. For example, “negative ten”:
class numstr: def __init__(self, value): self.value = value
Now you can create numstr objects:
n1 = numstr("thirteen")
Now, let’s customize this class such that when you call abs() on a numstr object, it removes the word “negative” from the number string:
class numstr: def __init__(self, value): self.value = value def __abs__(self): absolute = self.value.replace("negative", "") return absolute
Now you can call abs() on a numstr object to remove possible “negative” strings:
n2 = numstr("negative ten") print(abs(n2))
This demonstrates how you can customize the __abs__() method on your custom class.
Today you learned what the __abs__() method is in Python.
To recap, the __abs__() method specifies what happens when you call abs() on an object.
When talking about numbers, the absolute value means removing the negative sign in front of the number. For example, abs(-10) returns 10.
But if you have a custom class, the absolute value could mean something else. For example, if you represent numbers as strings, the absolute value would mean removing the word “negative” in front of the number string. This is why it is possible to implement a customized __abs__() method to specify what absolute value means in the context of that class.
About the Author
- I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.
- Artificial Intelligence2023.05.16Humata AI Review (2023): Best PDF Analyzer (to Understand Files)
- Python2023.05.139 Tips to Use ChatGPT to Write Code (in 2023)
- Artificial Intelligence2023.04.1114 Best AI Website Builders of 2023 (Free & Paid Solutions)