[Tutor] PYTHONHASHSEED, -R

Albert-Jan Roskam fomcl at yahoo.com
Mon Jul 29 22:29:09 CEST 2013


----- Original Message -----

> From: Steven D'Aprano <steve at pearwood.info>
> To: tutor at python.org
> Cc: 
> Sent: Monday, July 29, 2013 6:44 PM
> Subject: Re: [Tutor] PYTHONHASHSEED, -R
> 
> On 29/07/13 19:29, Albert-Jan Roskam wrote:
> 
>>>  The dict isn't changing state,
>> 
>>  So that's the criterion! Thanks! So as long as you don't use 
> __setitem__ and __delitem__ (maybe also __setattribute__, __delattribute__, ...) 
> the state does not change.
> 
> Built-in dicts cannot take extra attributes:
> 
> py> d = {}
> py> d.surprise = "what?"
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> AttributeError: 'dict' object has no attribute 'surprise'
> 
> and even if they did, it would be irrelevant for traversing the key/value pairs.
> 
> 
>>  and the table traversal in each case is
>>>  a single pass, looking for non-NULL values. I'm no language lawyer,
>>>  but it seems the wording in the docs under the description of views
>>>  guarantees that this is true across Python implementations:
>>> 
>>> 
> http://docs.python.org/2.7/library/stdtypes.html#dictionary-view-objects
> 
> Even before views, it has been a guarantee going as far back as I can remember 
> that keys(), values() and items() will operate in the same order so long as they 
> are called without modifying the dict between calls.
> 
> 
>>  >      Keys and values are iterated over in an arbitrary order which is
>>>       non-random,
>> 
>>  That sounds like a contradictio in terminis to me. How can something be 
> non-random and arbitrary at the same time?
> 
> 
> That's easy. Two nights ago, I had to decide between having fish or chicken 
> for dinner. For some reason, I decided on fish. I can't tell you why -- I 
> like both equally well, they both needed eating, I could have picked one or the 
> other. Although I had no problem deciding that I wanted the fish, I have no 
> insight into my reasons for that decision. Presumably there is something deep 
> inside my unconscious brain that led me to decide that "I feel like fish 
> tonight" rather than chicken, but since I'm totally unaware of the 
> reasons for that choice, it was an arbitrary choice.
> 
> Last night I had to choose between Italian or Chinese food. I really 
> couldn't decide, so I tossed a coin[1]. That was a random choice, and I had 
> no control over it. The result was entirely decided by the random process of 
> tossing a coin.
> 
> In the case of dicts, the iteration order is *deterministic* and follows from 
> the internal structure of the dictionary's hash table, but it is 
> unpredictable and arbitrary:

Nice analogy. Thanks. My wife usually decides too, although I do tend to emphasize that there has got to be beer. ;-)
 
<snip>


More information about the Tutor mailing list