Sorting distionary by value

David Bolen db3l at fitlinxx.com
Thu Mar 28 21:22:36 EST 2002


philh at comuno.freeserve.co.uk (phil hunt) writes:

> Correct. IMO "try to do something sensible" should be the default.

No - "do something sensible" (read "do what the programmer intended
where different programmers intend different things and the language
is supposed to somehow guess which is right") is more along the line
of what Perl attempts (and quite well in many cases).  It's a valid
approach, but doesn't fit with Pythons approach to things.

> But 9 times out of 10, you'll want to use a default of 0.

Only in cases where the dictionary entry is a counter which will be
incremented, which is certainly far less than 90% of the time in my
code.

Remember also that this sort of rule can rear up and bite you in some
complex expression sometime where it wasn't as "sensible" what the
default behavior should be and not having the dictionary key in place
was really an error that was then hidden from view until it causes a
subtle problem at some later point in time.

Others have also pointed out ways (including some built-in default
approaches with Python 2.2's dictionary class) that you can provide
such support yourself as needed.  It doesn't belong in the base
behavior.

> Having to explicitly state things like this is a sign of low 
> levelnmess in a lang. Next you'll be arguing that explicit storage 
> management is a good thing.... :-)

Totally different things.  I think we can both agree on what the
semantics are of obtaining storage for an object and releasing it when
the object is done.  But this has to do with locating an object - you
want Python to "guess" at what I really wanted to get back when I
asked for a non-existent dictionary entry.  I don't want it to guess.
Nothing to do with high level versus low level.

> I have never had maintainability problems with the other way round.

That need not extrapolate to a general rule.  Personally I found that
this sort of approach works well for small quick scripts where I
really just want the answer and can play fast and loose with the
details of the operations I am detailing, but breaks down over time
and size.  You may not agree, but that's why there are other languages
to choose from that do take your preferred approach by default :-)

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list