Python __round__() Magic Method

Rate this post

Syntax

object.__round__(self, ndigits=0)

The Python __round__() method implements the built-in round() function. For example, if you attempt to call round(x) or round(x, ndigits), Python will run the x.__round__() or x.__round__(ndigits) method, respectively.

The following code snippet overrides the __round__() dunder method to return the rounded age of a Person when you pass an object of type Person into the round() function:

class Person:
    def __init__(self, age):
        self.age = age

    def __round__(self, ndigits=0):
        return round(self.age)



alice = Person(42.42424242)
print(round(alice))
# 42

How to fix TypeError: type XXX doesn’t define __round__ method

Note that without defining the __round__() method, Python would’ve raised a TypeError:

class Person:
    def __init__(self, age):
        self.age = age


alice = Person(42.42424242)
print(round(alice))

Output:

Traceback (most recent call last):
  File "C:\Users\...\code.py", line 7, in <module>
    print(round(alice))
TypeError: type Person doesn't define __round__ method

To fix this TypeError, simply define the __round__() method as outlined in the first code snippet in this article.

We call this a “Dunder Method” for Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.

Background round()

Python’s built-in round() function takes two input arguments: a number and an optional precision in decimal digits. It rounds the number to the given precision and returns the result. The return value has the same type as the input number—or integer if the precision argument is omitted. Per default, the precision is set to 0 digits, so round(3.14) results in 3.

Here are some examples:

>>> round(3.14)
3
>>> round(3.14, ndigits=1)
3.1
>>> round(3.13, ndigits=-1)
0.0
>>> round(4458.1242, ndigits=-1)
4460.0
>>> round(3.14159, ndigits=3)
3.142

To understand this operation in detail, feel free to read over our tutorial or watch the following video:

References:

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!