## Syntax and Definition

`object.__trunc__(self)`

The Python `__trunc__()`

method implements the behavior of the `math.trunc()`

function. For example, if you attempt to call `math.trunc(x)`

, Python will run the `x.__trunc__()`

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 built-in function `int()`

falls back to `__trunc__()`

if both magic methods `__int__()`

and `__index__()`

are undefined.

## Example

The following code snippet overrides the `__trunc__()`

dunder method to return the “rounded down” age of a `Person`

when you pass an object of type `Person`

into the `math.trunc()`

function:

import math class Person: def __init__(self, age): self.age = age def __trunc__(self): return 99 alice = Person(42.99999) print(math.trunc(alice)) # 99 bob = Person(42.0) print(math.trunc(bob)) # 99

## How to fix “TypeError: type XXX doesn’t define __trunc__ method”?

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.trunc(alice))

Output:

Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 11, in <module> print(math.trunc(alice)) TypeError: type Person doesn't define __trunc__ method

To fix this `TypeError`

, simply define the `__trunc__()`

method as outlined in the first code snippet in this article.

## Background

The **truncation function** takes a real number `x`

and returns its integer part `n`

. (Unlike `ββ
β`

and `ββ
β`

, there isn’t a standard way of writing the truncation function.)

The `math.trunc()`

method does the same thing as `int()`

applied to floats. If you write a number `x`

as a decimal, then `math.trunc()`

returns the integer part to the left of the decimal point. Its output has class `int`

.

import math lst = [1.5, 3, -6.2, math.pi, 0, 2.71828, 29.0 , -91.312, math.sqrt(2)] for x in lst: print("math.trunc(" + str(x) + "): " + str(math.trunc(x)))

Output:

math.trunc(1.5): 1 math.trunc(3): 3 math.trunc(-6.2): -6 math.trunc(3.141592653589793): 3 math.trunc(0): 0 math.trunc(2.71828): 2 math.trunc(29.0): 29 math.trunc(-91.312): -91 math.trunc(1.4142135623730951): 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.

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.