Sorting distionary by value

phil hunt philh at comuno.freeserve.co.uk
Thu Mar 28 19:43:54 EST 2002


On Thu, 28 Mar 2002 12:01:15 -0800, Jeff Shannon <jeff at ccvcorp.com> wrote:
>
>
>phil hunt wrote:
>
>> IIRC in Awk you can just say:   freq[word] ++ and it works
>> correctly even when there is no pre-existing index of word in freq.
>>
>> IMO it's a pity Python isn't like that.
>
>I disagree.  I'm just as happy that Python doesn't try to guess what would be a
>suitable "default" value for non-existent keys.  

The default value should always be None.

None should act like 0 or "" or [] or {} where appropriate, i.e.:

   None + 1 --> 1
   None + "x" --> "x"
   None * 4 --> 0
   None + ['x'] --> ['x']
   None + {2:3} --> {2:3}

etc.

(Note I am implying here that + should work with dictionaries, i.e.
{2:3} + {4:5} --> {2:3, 4:5})

>It seems to me to be entirely
>consistent with the philosophy of "explicit is better than implicit".

But that isn't the philosophy of any high level langauge. In fact, 
the more high level any language is, the more implicit things are. 
And I would argue that's a pretty good definition of what a high 
level language is.

>  Why
>presume that somedict[key] should default to 0, instead of to an empty string,
>or to None, or to [], or...  ???

See above

-- 
<"><"><"> Philip Hunt <philh at comuno.freeserve.co.uk> <"><"><">
"I would guess that he really believes whatever is politically 
advantageous for him to believe." 
                        -- Alison Brooks, referring to Michael
                              Portillo, on soc.history.what-if



More information about the Python-list mailing list