[Python-ideas] Dict literal use for custom dict classes
Jelte Fennema
me at jeltef.nl
Tue Dec 15 08:27:14 EST 2015
I see your point, but it would almost never make sense to list the
attributes of a namedtuple. As this would be the only one that could cause
confusion (since OrderedDict would do the same as dict), I doubt it would
actually cause that much confusion.
Jelt
On 15 December 2015 at 14:19, Chris Angelico <rosuav at gmail.com> wrote:
> On Wed, Dec 16, 2015 at 12:08 AM, Jelte Fennema <me at jeltef.nl> wrote:
> > After thinking some more, I think you are right in saying that it would
> make
> > more sense to let it represent an OrderedDict directly. Mostly because
> the
> > mutability suggested by the square brackets. And also a bit because I'm
> not
> > sure when a mapping that maps multiple values to the same key is actually
> > useful.
> >
> > Secondly, I think your idea for namedtuple literals is great. This would
> be
> > really useful in the namedtuple use case where you want to return
> multiple
> > values from a function, but you want to be clear in what these values
> > actually are. I think this would need to generate some kind of anonymous
> > named tuple class though, since it would make no sense to have to create
> a
> > new class when using a literal like this.
>
> Be careful of this trap, though:
>
> >>> from collections import namedtuple, OrderedDict
> >>> Point = namedtuple('Point', ['x', 'y'])
> >>> p = Point(x=5, y=2)
> >>> list(p)
> [5, 2]
> >>> od = OrderedDict((('x',5),('y',2)))
> >>> list(od)
> ['x', 'y']
>
> Dictionary-like things iterate over their keys; tuple-like things
> iterate over their values. (And a list of pairs would effectively
> iterate over items().) Having extremely similar syntax for creating
> them might well lead to a lot of confusion on that point.
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151215/1b2503a7/attachment-0001.html>
More information about the Python-ideas
mailing list