How well do you know Python?

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Tue Jul 5 05:51:07 EDT 2016


Chris Angelico writes:

> On Tue, Jul 5, 2016 at 6:36 PM, Peter Otten wrote:
>> It looks like
>>
>> $ python3 -c 'print({1, 2})'
>> {1, 2}
>> $ python3 -c 'print({2, 1})'
>> {1, 2}
>>
>> will always print the same output. Can you construct a set from two small
>> integers where this is not the case? What's the difference?
>
> Given that the display (iteration) order of sets is arbitrary, I'm not
> sure what the significance would ever be, but my guess is that the
> display order would be the same for any given set, if constructed this
> way. But it sounds as if you know of a set that behaves differently.

The first thing that came to mind, {-1,-2} and {-2,-1}.

But I haven't a clue. It doesn't happen with -1 and -3, or with another
pair that I tried, and it doesn't seem to be about object identity.

>> What happens if you replace the ints with strings? Why?
>
> Then hash randomization kicks in, and you can run the exact same line
> of code multiple times and get different results. It's a coin toss.

Oh, nice, a new way to generate random bits in shell scripts.



More information about the Python-list mailing list