converting from perl: variable sized unpack

phil hunt philh at comuno.freeserve.co.uk
Wed Jul 18 14:20:54 CEST 2001


On 17 Jul 2001 22:20:39 GMT, Mike Orr <iron at mso.oz.net> wrote:
>phil hunt <philh at comuno.freeserve.co.uk> wrote:
>> However, im my experience, most of the time you don't want to do this,
>> most of the time, if there are too few or many values, you weant the
>> software to "do its best". 
>
>Perl "does its best" by making guesses about what you want.  Python is
>more deterministic, which means you have a greater certainty about what
>the program will actually do.

Python would still be deterministic if:

(a,b,c,d) = (1,2,3)

resulted in d being None

Ditto if (10,11,12)[3] returned None instead of an exception.

If people *want* an exception in instances liek this, they should
be able to say something like:

   if not myCollection.hasIndex(x): raise Exception

(Note that hasIndex() should equally work for dictionaries as well
as sequence-collections (strings, tuples, lists, etc). IMO it is
an unnecessary non-orthogonality of the present python that it doesn't
do this)

>> This goes for lots of things; IMO if you
>> access a list or dictionary with a non-existant index it should
>> return a value such as None, instead of raising an exception.
>
>Sometimes.  It's convenient in CGI to have tags default to '' if missing.
>But you can't find a one-size-fits all solution.  For some applications,
>None and '' are legitimate values, and must be distinguished from
>"key not found".  Thus, why Python offers both dict.get('x', DEFAULT)
>as well as dict['better be there or KeyError'].

No, because on the odd occasions that you want an exception you can
always throw one manually.

Here, I'm assuming that usually you don't want one -- perhaps this
is jsut my idiosyncratic programming style and most people vare different
here. Comments?

-- 
#===== Philip Hunt == philh at comuno.freeserve.co.uk ======#
    Herbivore: effort-free public key encryption. See:
<http://www.vision25.demon.co.uk/oss/herbivore/intro.html>
        ** First software release coming soon! **






More information about the Python-list mailing list