__abs__() method in Python Explained in Detail
Posted in Python on January 22, 2023 by Jessica Rose ‐ 4 min read

abs()
method in python is used to get the absolute value in python, abs() method is an in-built function.
The absolute value of a number is its distance from zero on the number line, regardless of whether the number is positive or negative. For example, the absolute value of -5 is 5, and the absolute value of 5 is also 5.
For example :
n = -1000
print(abs(n))
>>> abs(-5)
5
>>> abs(5.2)
5.2
>>> abs(-3.14)
3.14
>>> abs(3+4j)
5.0
Output :
1000
The result will be an absolute value, which is a positive value.
It’s crucial to recognize that invoking abs(n) is equivalent to calling n.__abs__()
.
For example:
n = -1000
print(abs(n))
print(n.__abs__())
Output:
1000
1000
## Double Underscore methods in Python [Explained] :
In Python, methods with double underscores, also known as “dunder methods,” are used for operator overloading.
These methods are implemented within classes to define how an object behaves when a specific operation is performed on it.
A well-known dunder method is the init method, which is automatically executed when an object is instantiated.
class Student:
def __init__(self, name):
self.name = name
# This calls __init__ with "name" argument:
student1 = Student("Elon Musk")
print(student1.name) # prints "Elon Musk"
The add method, also known as a “dunder method,” is a common way to define the behavior of the + operator when used with objects of a particular class.
This method allows you to specify how two objects should be added together, providing a clear and consistent way to manipulate objects of that class.
class Student:
def __init__(self, name):
self.name = name
def __add__(self, otherfruit):
# Creates a new Student with the two names combined
return Student(self.name + otherfruit.name)
a = Student("Elon Musk")
b = Student("Jeff Bezos")
combo = a + b
print(combo.name) # Prints "Elon MuskJeff Bezos"
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 = a + b
We can alternatively call with :
combo = a.__add__(b)
How do abs() Method work in Python :
The abs() method in Python is a built-in function that returns the absolute value of an object.
Additionally, it can be implemented as a special method, known as abs(), in custom classes to define the behavior of the abs() function when applied to objects of that class.
The absolute value of a number represents its distance from zero, effectively removing any negative sign.
This concept applies to all numerical data types, including integers, which have an implemented abs() method that returns the absolute value when the abs() function is called on an integer.
In layman’s terms, an absolute value removes the negative sign from a negative number.
As seen in the above examples.
Customizing abs() Method in Your Class as per your requirement
Let’s create custom __abs__()
method for a class, that represents numbers as string:
class NumberString:
def __init__(self, value):
self.value = value
def __abs__(self):
if self.value.startswith('-'):
return NumberString(self.value[1:])
else:
return NumberString(self.value)
num_string = NumberString("-123")
print(abs(num_string)) # prints "123"
In this example, we define a class NumberString that initializes a number as a string.
The
__abs__()
method checks if the string starts with a negative sign “-”, if it does it will remove it using string slicing and return the new string as a NumberString object.If the string does not start with a negative sign, it returns the same string as a NumberString object.
Here, the absolute value of the number represented as a string is represented as the same number without the negative sign.
You can also use this method with other classes and data types to customize the meaning of absolute value.
Conclusion
The absolute value of a number is its distance from zero on the number line, disregarding its positive or negative sign.
In Python, the abs() method can be used to obtain the absolute value of a number. For example, abs(-10) returns 10.
However, when working with custom classes, the meaning of absolute value may differ.
For instance, if numbers are represented as strings, the absolute value may refer to removing the word “negative” from the number string.
To handle such scenarios, it is possible to define a custom __abs__()
method that specifies the meaning of absolute value in the context of the class.