Syntax and Definition
__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.
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
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
import math class Person: def __init__(self, age): self.age = age alice = Person(42.99999) print(math.floor(alice))
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.
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
−10 (rather than
If we rephrase this in terms of the integer part
x, we get
You can read more in our full guide:
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.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.