Created
March 15, 2017 21:12
-
-
Save nazavode/ac78672e3a4513549ed1a8d15b4bb51a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from collections import namedtuple, Mapping | |
__all__ = ['defaultuple'] | |
def defaultuple(typename, field_names, default_values=()): | |
""" Build a new `namedtuple` type with optional default values that | |
can be specified as positional or keyword arguments. | |
""" | |
T = namedtuple(typename, field_names) | |
T.__new__.__defaults__ = (None,) * len(T._fields) | |
if isinstance(default_values, Mapping): | |
prototype = T(**default_values) | |
else: | |
prototype = T(*default_values) | |
T.__new__.__defaults__ = tuple(prototype) | |
return T |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment