[Python-ideas] creating dicts with duplicates on purpose [was Re: dictionary constructor should not allow duplicate keys]
Ethan Furman
ethan at stoneleaf.us
Wed May 4 12:06:24 EDT 2016
On 05/04/2016 07:54 AM, Paul Moore wrote:
> On 4 May 2016 at 13:42, Ethan Furman wrote:
>> On 05/04/2016 02:48 AM, Paul Moore wrote:
>>> The problem is that it could also break code that works at the moment.
>>> Consider a mapping of constants to names:
>>>
>>> colors = {
>>> RED: "Red",
>>> BLUE: "Blue",
>>> PINK: "Pink",
>>> LIGHT_BLUE: "Light Blue",
>>> DARK_BLUE: "Dark Blue",
>>> }
>>>
>>> On a certain output device, there may be no "light blue", and in that
>>> case the code sets LIGHT_BLUE = BLUE. That's entirely reasonable, and
>>> the author may be perfectly happy with the current behavior in that
>>> case.
>>
>> Actually, that code is setting BLUE = "Light Blue". ;)
>
> You missed my point, I think. BLUE and LIGHT_BLUE are "constant"
> values (set once at the top of the program).
No, I got your point, and I agree. But one of us is confused about your
explanation of that example (it could easily be me). Here's how I think
it works:
RED = 1
BLUE = 2
PINK = 3
LIGHT_BLUE = 4
DARK_BLUE = 5
# oops, light blue not supported on this machine, just use blue
LIGHT_BLUE = 2
>>> colors = {
RED: "Red",
BLUE: "Blue",
PINK: "Pink",
LIGHT_BLUE: "Light Blue",
DARK_BLUE: "Dark Blue",
}
>>> colors
{1: 'Red', 2: 'Light Blue', 3: 'Pink', 5: 'Dark Blue'}
So my issue was not with your point, but with your explanation of the
example of your point.
--
~Ethan~
More information about the Python-ideas
mailing list