[Python-ideas] namedtuple fields with default values

Antoine Pitrou solipsis at pitrou.net
Fri Jul 17 17:33:57 CEST 2015

On Fri, 17 Jul 2015 10:44:15 -0400
Barry Warsaw <barry at python.org> wrote:
> I don't know about "Pythonic" but there's a not too horrible way to do it:
> _Record = namedtuple('Record', 'url destination checksum')('', '', '')
> def Record(url, destination, checksum=''):
>     return _Record._replace(
>         url=url, destination=destination, checksum=checksum)

My usual pattern is to subclass the namedtuple class and override the
constructor (especially if I want to add behaviour):

class Record(namedtuple('_Record', ('url', 'dest', 'checksum'))):
    __slots__ = ()

    def __new__(...): #etc



More information about the Python-ideas mailing list