On Wed, Nov 30, 2016 at 7:09 AM, Ethan Furman <et...@stoneleaf.us> wrote:

> On 11/30/2016 02:32 AM, Jelte Fennema wrote:
>
> 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
>>
>
> 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,
>

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>)
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to