[Python-ideas] Fwd: Anonymous namedtuples

Joseph Martinot-Lagarde contrebasse at gmail.com
Tue Apr 19 16:25:30 EDT 2016


Guido van Rossum <guido at ...> writes:

> A general warning about this (x=12, y=16) idea: It's not so different from
using dicts as cheap structs, constructing objects on the fly using either
{'x': 12, 'y': 16} or dict(x=12, y=16). The problem with all these this is
that if you use this idiom a lot, you're invariably going to run into cases
where a field is missing, and you're spending a lot of time tracking down
where the object was created incorrectly. Using a namedtuple (or any other
construct that asks you to pre-declare the type used) the incorrect
construction will cause a failure right at the point where it's being
incorrectly constructed, making it much simpler to diagnose.

I think that it depends a lot on what these are used for. What I primarily
though about was to be able to easily return a namadtuple as a function
output. In this case the namedtuple (anonymous or not) is created in a
single place, so having to declare the namedtuple before using it has a
limited interest, and has the drawbacks I presented before (code
duplication, declaration far away from the use).

What you're talking about is more like a container which would be used all
around an application, where you need to ensure the corecntess of the struct
everywhere. In this case a standard nametuple is a better fit.

I feel like it's similar to the separation you talked about before, script
vs application. My view is more script-like, yours is more application-like.

Joseph



More information about the Python-ideas mailing list