Python __trunc__() Magic Method

Rate this post

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.

Join the free webinar now!