<div dir="ltr"><div>I'm using a namedtuple to keep track of several fields, only some of which ever need to be specified during instantiation. However, there is no Pythonic way to create a namedtuple with fields that have default values.</div><div><br></div><div><font face="monospace, monospace">>>> Foo = namedtuple('Foo', ['bar', 'optional_baz'])</font></div><div><font face="monospace, monospace">>>> f = Foo('barValue') # Not passing an argument for every field will cause a TypeError</font></div><div><font face="monospace, monospace">Traceback (most recent call last):</font></div><div><font face="monospace, monospace">  File "<stdin>", line 1, in <module></font></div><div><font face="monospace, monospace">TypeError: __new__() takes exactly 3 arguments (2 given)</font></div><div><br></div><div>If you do want default parameters for a namedtuple, the workaround right now involves modifying Foo.__new__'s defaults:</div><div><font face="monospace, monospace">>>> Foo = namedtuple('Foo', ['bar', 'optional_baz'])</font></div><div><font face="monospace, monospace">>>> Foo.__new__.__defaults__ = (None, None)</font></div><div><br></div><div>Then you can call Foo's constructor without specifying each field:</div><div><font face="monospace, monospace">>>> f = Foo('barValue')</font></div><div><font face="monospace, monospace">>>> f</font></div><div><font face="monospace, monospace">Foo(bar='barValue', optional_baz=None)</font></div><div><br></div><div>Having to assign to Foo.__new__.__defaults__ is a bit ugly. I think it would be easier and more readable to support syntax like:</div><div><font face="monospace, monospace">>>> Foo = namedtuple('Foo', ['optional_bar=None', 'optional_baz=None'])</font></div><div><br></div><div>This suggestion is fully backwards compatible and allows for cleaner definitions of nametuples with default-value fields. Thanks for considering.</div><div><br></div><div>Russell</div></div>