Syntax and Description
__ceil__() method implements the behavior of the
math.ceil() function. For example, if you attempt to call
math.ceil(x), Python will run the
x.__ceil__() 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
__ceil__() dunder method to return the “rounded up” 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 __ceil__(self): floor_value = int(self.age) if floor_value < self.age: return floor_value + 1 return floor_value alice = Person(42.42424242) print(math.ceil(alice)) # 43 bob = Person(42.0) print(math.ceil(bob)) # 42
How to fix “TypeError: must be real number, not XXX”?
Note that without defining the
__ceil__() method, Python would’ve raised a
import math class Person: def __init__(self, age): self.age = age alice = Person(42.42424242) print(math.ceil(alice))
Traceback (most recent call last): File "C:\Users\...\code.py", line 12, in <module> print(math.ceil(alice)) TypeError: must be real number, not Person
To fix this
TypeError, simply define the
__ceil__() method as outlined in the first code snippet in this article.
Next we will look at the ceil function
⌈⋅⌉. Just as the floor function is the real number
x rounded down,
⌈x⌉ is just
x “rounded up”, i.e. the smallest integer greater than
For instance, if
x=9.1, then the ceil of
x is just
10. On the other hand, if
x=−9.1, then the smallest integer greater than
x is an integer, then its ceil is just itself.
If we phrase this in terms of the integer and fractional part from before, we get
You can see from the above discussion that if
x is not an integer, then
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. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, 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.