[Python-ideas] namedtuple literals [Was: RE a new namedtuple]
Stephen J. Turnbull
turnbull.stephen.fw at u.tsukuba.ac.jp
Tue Jul 25 05:08:18 EDT 2017
C Anthony Risinger writes:
> At the end of the day, I don't see a way to have both a literal and
> something that is externally "named", because the only ways to pass the
> name I can imagine would make it look like a value within the container
> itself (such as using a literal string for the first item), unless even
> more new syntax was added.
OK, so I took your "a tuple is a tuple is a tuple" incorrectly. What
you want (as I understand it now) is not what
def ntuple0(attr_list):
return namedtuple("_", attr_list)
gives you, but something like what
def ntuple1(attr_list)
return namedtuple("ImplicitNamedtuple_" + "_".join(attr_list),
attr_list)
does. Then this would truly be a "duck-typed namedtuple" as Chris
Barker proposed in response to Steven d'Aprano elsewhere in this
thread. See also Nick's full, namedtuple-compatible, implementation.
Of course we still have the horrible "list of strings naming
attributes" argument, so you still want a literal if possible, but
with a **key argument, a new builtin would do the trick for me. YMMV.
--
Associate Professor Division of Policy and Planning Science
http://turnbull/sk.tsukuba.ac.jp/ Faculty of Systems and Information
Email: turnbull at sk.tsukuba.ac.jp University of Tsukuba
Tel: 029-853-5175 Tennodai 1-1-1, Tsukuba 305-8573 JAPAN
More information about the Python-ideas
mailing list