Add optional defaults to namedtuple

It would be nice to have a supported way to add defaults to namedtuple, so the slightly hacky solution here does not have to be used: http://stackoverflow.com/a/18348004/2570866 Jelte

On 11/30/2016 02:32 AM, Jelte Fennema wrote:
Actually, the solution right below it is better [1]: --> from collections import namedtuple --> class Node(namedtuple('Node', ['value', 'left', 'right'])): --> __slots__ = () --> def __new__(cls, value, left=None, right=None): --> return super(Node, cls).__new__(cls, value, left, right) But even more readable than that is using the NamedTuple class from my aenum [3] library (and on SO as [3]): --> from aenum import NamedTuple --> class Node(NamedTuple): --> val = 0 --> left = 1, 'previous Node', None --> right = 2, 'next Node', None shamelessly-plugging-my-own-solutions'ly yrs, -- ~Ethan~ [1] http://stackoverflow.com/a/16721002/208880 [2] https://pypi.python.org/pypi/aenum [3] http://stackoverflow.com/a/40891597/208880

On Wed, Nov 30, 2016 at 7:09 AM, Ethan Furman <ethan@stoneleaf.us> wrote:
Ditto: with PEP 526 and the latest typing.py (in 3.6) you will be able to do this: class Employee(NamedTuple): name: str id: int We should make it so that the initial value in the class is used as the default value, too. (Sorry, this syntax still has no room for a docstring per attribute.) -- --Guido van Rossum (python.org/~guido <http://python.org/%7Eguido>)

On 30 November 2016 at 13:09, Ethan Furman <ethan@stoneleaf.us> wrote:
Sorry - taking the boat to even-more-shamelessly anounce extradict.extratuple.defaultnamedtuple - in the newly released extradict v. 0.2.5 https://pypi.python.org/pypi/extradict/0.2.5 It allows one to build an default-paremetrized namedtuple by passing a sequence of 2-tuples with key, values, or, on Python 3.6, pass in the default values as keywords to the defaultnamedtuple factory. (The "extradict" package, with a faster reimplementation of namedtuple already existed, of course - maybe someone can pick some other weird idea I have into there to put it into more day-to-day use)
-- ~Ethan~

On 11/30/2016 02:32 AM, Jelte Fennema wrote:
Actually, the solution right below it is better [1]: --> from collections import namedtuple --> class Node(namedtuple('Node', ['value', 'left', 'right'])): --> __slots__ = () --> def __new__(cls, value, left=None, right=None): --> return super(Node, cls).__new__(cls, value, left, right) But even more readable than that is using the NamedTuple class from my aenum [3] library (and on SO as [3]): --> from aenum import NamedTuple --> class Node(NamedTuple): --> val = 0 --> left = 1, 'previous Node', None --> right = 2, 'next Node', None shamelessly-plugging-my-own-solutions'ly yrs, -- ~Ethan~ [1] http://stackoverflow.com/a/16721002/208880 [2] https://pypi.python.org/pypi/aenum [3] http://stackoverflow.com/a/40891597/208880

On Wed, Nov 30, 2016 at 7:09 AM, Ethan Furman <ethan@stoneleaf.us> wrote:
Ditto: with PEP 526 and the latest typing.py (in 3.6) you will be able to do this: class Employee(NamedTuple): name: str id: int We should make it so that the initial value in the class is used as the default value, too. (Sorry, this syntax still has no room for a docstring per attribute.) -- --Guido van Rossum (python.org/~guido <http://python.org/%7Eguido>)

On 30 November 2016 at 13:09, Ethan Furman <ethan@stoneleaf.us> wrote:
Sorry - taking the boat to even-more-shamelessly anounce extradict.extratuple.defaultnamedtuple - in the newly released extradict v. 0.2.5 https://pypi.python.org/pypi/extradict/0.2.5 It allows one to build an default-paremetrized namedtuple by passing a sequence of 2-tuples with key, values, or, on Python 3.6, pass in the default values as keywords to the defaultnamedtuple factory. (The "extradict" package, with a faster reimplementation of namedtuple already existed, of course - maybe someone can pick some other weird idea I have into there to put it into more day-to-day use)
-- ~Ethan~
participants (5)
-
Ethan Furman
-
Guido van Rossum
-
Jelle Zijlstra
-
Jelte Fennema
-
Joao S. O. Bueno