Python __ceil__() Magic Method

Rate this post

Syntax and Description

``object.__ceil__(self)``

The Python `__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.

Example

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 `math.ceil()` function:

```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 `TypeError`:

```import math

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

alice = Person(42.42424242)
print(math.ceil(alice))```

Output:

```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.

Background

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 `x`.

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 `−9`, so `⌈x⌉=−9`. If `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 `⌈x⌉=⌊x⌋+1`

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!