Re: [Python-ideas] Add optional defaults to namedtuple

2016-12-01 Thread Jelle Zijlstra
2016-11-30 8:11 GMT-08:00 Guido van Rossum :

> On Wed, Nov 30, 2016 at 7:09 AM, Ethan Furman  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.)
>
> Implemented this in https://github.com/python/typing/pull/338

> --
> --Guido van Rossum (python.org/~guido )
>
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Add optional defaults to namedtuple

2016-11-30 Thread Joao S. O. Bueno
On 30 November 2016 at 13:09, Ethan Furman  wrote:
> 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,

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


Re: [Python-ideas] Add optional defaults to namedtuple

2016-11-30 Thread Guido van Rossum
On Wed, Nov 30, 2016 at 7:09 AM, Ethan Furman  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 )
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Add optional defaults to namedtuple

2016-11-30 Thread Ethan Furman

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,
--
~Ethan~


[1] http://stackoverflow.com/a/16721002/208880
[2] https://pypi.python.org/pypi/aenum
[3] http://stackoverflow.com/a/40891597/208880
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/