Python __floor__() Magic Method

Rate this post

Syntax and Definition

object.__floor__(self)

The Python __floor__() method implements the behavior of the math.floor() function. For example, if you attempt to call math.floor(x), Python will run the x.__floor__() method to obtain the return value.

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.

Example

The following code snippet overrides the __floor__() dunder method to return the “rounded down” age of a Person when you pass an object of type Person into the math.floor() function:

import math


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

    def __floor__(self):
        floor_value = int(self.age)
        return floor_value



alice = Person(42.99999)
print(math.floor(alice))
# 42

bob = Person(42.0)
print(math.floor(bob))
# 42

How to fix “TypeError: must be real number, not XXX”?

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

import math


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


alice = Person(42.99999)
print(math.floor(alice))

Output:

Traceback (most recent call last):
  File "C:\Users\...\code.py", line 10, in <module>
    print(math.floor(alice))
TypeError: must be real number, not Person

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

Background

Let’s first recall what the floor function ⌊⋅⌋ does in mathematical terms. For a real number x, its floor function ⌊x⌋ is just x “rounded down”, i.e. the largest integer not exceeding x. In particular, if x is an integer, then its floor is just itself.

For instance, if x=9.1, then the floor of x is just 9. On the other hand, if x=−9.1, then the largest integer not exceeding x is −10 (rather than −9), so ⌊x⌋=−10.

If we rephrase this in terms of the integer part n of x, we get

You can read more in our full guide:

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!