converting from perl: variable sized unpack
phil hunt
philh at comuno.freeserve.co.uk
Thu Jul 19 07:41:03 EDT 2001
On Wed, 18 Jul 2001 23:48:01 +0200, Alex Martelli <aleaxit at yahoo.com> wrote:
>"phil hunt" <philh at comuno.freeserve.co.uk> wrote in message
>news:slrn9lavp6.v3.philh at comuno.freeserve.co.uk...
> ...
>> 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)
>
>It's quite orthogonal if you think of exceptions as the MAIN way
>to communicate the missing index case:
>
>def hasindex(myCollection, x):
> try: myCollection[x]
> except LookupError: return 0
> else: return 1
>
>LookupError is the common base class of IndexError, raised by
>sequences, and KeyError, raised by mappings.
No, I mean:
>>> a=[1,66,77,88]
>>> a.has_key(2)
should return 1 not:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: has_key
>> No, because on the odd occasions that you want an exception you can
>> always throw one manually.
>
>And viceversa, if the odd occasions are those in which you
>want to SUPPRESS the exception, you can easily try/except it.
On the contrary, i want to suppress the exception >90% of the
time.
--
#===== 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