<div dir="ltr">FWIW, PEP 484 (and hence typing.py) uses this syntax with a different meaning:<br><br>* NamedTuple, used as<br>  ``NamedTuple(type_name, [(field_name, field_type), ...])``<br>  and equivalent to<br>  ``collections.namedtuple(type_name, [field_name, ...])``.<br>  This is useful to declare the types of the fields of a a named tuple<br>  type.<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 17, 2015 at 4:59 PM, Eric V. Smith <span dir="ltr"><<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">-----BEGIN PGP SIGNED MESSAGE-----<br>
Hash: SHA1<br>
<div><div class="h5"><br>
On 07/17/2015 10:44 AM, Barry Warsaw wrote:<br>
> On Jul 16, 2015, at 01:03 PM, Russell Kaplan wrote:<br>
><br>
>> I'm using a namedtuple to keep track of several fields, only<br>
>> some of which ever need to be specified during instantiation.<br>
>> However, there is no Pythonic way to create a namedtuple with<br>
>> fields that have default values.<br>
><br>
> I don't know about "Pythonic" but there's a not too horrible way<br>
> to do it:<br>
><br>
> _Record = namedtuple('Record', 'url destination checksum')('', '',<br>
> '')<br>
><br>
> def Record(url, destination, checksum=''): return _Record._replace(<br>
> url=url, destination=destination, checksum=checksum)<br>
><br>
> Now you only need to provide 'url', and 'destination' when you<br>
> create a Record.  Okay, sure _Record is the actual namedtuple, but<br>
> I usually don't care.<br>
><br>
>> Having to assign to Foo.__new__.__defaults__ is a bit ugly. I<br>
>> think it would be easier and more readable to support syntax<br>
>> like:<br>
>>>>> Foo = namedtuple('Foo', ['optional_bar=None',<br>
>>>>> 'optional_baz=None'])<br>
><br>
> That would mean you couldn't ever have an actual parameter called<br>
> 'optional_bar'.<br>
><br>
>> This suggestion is fully backwards compatible and allows for<br>
>> cleaner definitions of nametuples with default-value fields.<br>
>> Thanks for considering.<br>
><br>
> Not that I think anything really needs to be done, but a few other<br>
> approaches could be:<br>
><br>
> * Allow for arbitrary keyword arguments at the end of the<br>
> signature to define default values.<br>
><br>
> Record = namedtuple('Record', 'url destination checksum',<br>
> checksum='')<br>
><br>
> * Extend the semantics of field_names to allow for a dictionary of<br>
> attributes mapping to their default values, though you'd need a<br>
> marker to be able to specify a required field:<br>
><br>
> Record = namedtuple('Record', dict(url=Required,<br>
> destination=Required, checksum=''))<br>
><br>
> I suppose I'd prefer the former, although that might cut off the<br>
> ability to add other controlling arguments to the namedtuple()<br>
> API.<br>
<br>
</div></div>I've implemented default parameters to namedtuples (and namedlists a<br>
mutable version) in <a href="https://pypi.python.org/pypi/namedlist" rel="noreferrer" target="_blank">https://pypi.python.org/pypi/namedlist</a><br>
<br>
The syntax is not great, but none of the options are awesome.<br>
<br>
>>> Point = namedlist.namedtuple('Point', [('x', 0), ('y', 100)])<br>
>>> p = Point() assert p.x == 0 assert p.y == 100<br>
<br>
<br>
<br>
-----BEGIN PGP SIGNATURE-----<br>
Version: GnuPG v1.4.14 (GNU/Linux)<br>
<br>
iQEcBAEBAgAGBQJVqRg5AAoJENxauZFcKtNxLSYH/iC2Twf2xYhAbtKdMF7nSvG2<br>
xOhhePDOBWl4K9cUVgg0z4jRsnGgL8O8FPCGVmsrBu8arseVAQbsFiAcOsmlKy3k<br>
XZGYQ61KPlDUcqxIO661cOWDiRJG/I9ltQHlafODEs4qGJfox2BeM5aCo+cIpyhk<br>
uC7wJ/t9mJ9uKvv6mG/e1GixzKtgcCO86NfYwIqiwaZWsKnjkqNzQ1peKm6pGRTK<br>
34u7oCu6EGbqNCUdAJx9Re6Umcs37FH/f3uyc2luJyP9z1p1zz2Ndb00NwOvsGKK<br>
NJw5enVB6/qC+sGZDicE1lJmij/MwdTbu/+Fl+JxjZeMGeRWJ9PEyHaxmuZAzw8=<br>
=ZXBa<br>
-----END PGP SIGNATURE-----<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div>