[Python-ideas] dictionary constructor should not allow duplicate keys

Random832 random832 at fastmail.com
Tue May 3 22:32:46 EDT 2016


On Tue, May 3, 2016, at 20:23, Steven D'Aprano wrote:
> On Tue, May 03, 2016 at 02:27:16PM -0700, Ethan Furman wrote:
> > On 05/03/2016 01:43 PM, Michael Selik wrote:
> > >How would you handle an expression that evaluates differently for each
> > >call? For example:
> > >
> > >     {random(): 0, random(): 1}
> > 
> > Easy:  Don't Do That.  ;)
> 
> I see your wink, so I presume you're not actually suggesting that the 
> compiler (1) prohibit all function calls in dict displays, or (2) 
> hard-code the function name "random" in a black list.

How about prohibit it anyway (on the grounds of the two key expressions
being identical - no comment on spam(a) vs spam(b)), and if the user
*really* wants it they'll do {random(): x for x in (0, 1)} [or {other:
stuff, **{that}}]


More information about the Python-ideas mailing list