How well do you know Python?

Steven D'Aprano steve at pearwood.info
Tue Jul 5 06:35:45 EDT 2016


On Tue, 5 Jul 2016 06: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?

Define "small". According to some mathematicians, any number you can write
down counts as small :-)


> What happens if you replace the ints with strings? Why?

Depends on the version of Python. Recent versions have hash randomisation
turned on, so the order of identical sets/dicts running in identical code
will vary from one run to another.


[steve at ando 3.6]$ ./python -c "print({'a', 'b', 'c', 'd'})"
{'c', 'b', 'd', 'a'}
[steve at ando 3.6]$ ./python -c "print({'a', 'b', 'c', 'd'})"
{'b', 'c', 'a', 'd'}
[steve at ando 3.6]$ ./python -c "print({'a', 'b', 'c', 'd'})"
{'c', 'a', 'd', 'b'}




-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list