DEV Community

Ari Abramowitz
Ari Abramowitz

Posted on

Namedtuple Default Values

I've been making my way through a deep-dive python series on Udemy (I highly recommend it - Python 3: Deep Dive by Dr. Fred Baptiste). The course recently touched on the question of how to set default arguments on a namedtuple. I found out afterwards that as of python 3.7 namedtuple has a defaults argument (see the docs), making the following neat little trick largely irrelevant. We'll pretend that doesn't exist for the moment.

Dr. Baptiste's way around the problem relies on the __new__ method and the __defaults__ property, neither of which typically get much air time:

  • __new__ is a built in method on all classes. It creates an instance of the class (before __init__ gets called)
  • __defaults__ is a property on all functions that stores the default values, and it's writable
    • if there are fewer defaults than function arguments, the values get "right aligned". See the screenshot: Image description

If you're on python 3.7+, stick with the built-in option. If not though, good to know you can do this:

from collections import namedtuple

Point = namedtuple("Point", ["x", "y", "z"])
# Defaults `y` to 2, and `z` to 3. `x` has no default
Point.__new__.__defaults__ = (2, 3)

print(Point(1))
# Point(x=1, y=2, z=3)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)