changing the List's behaviour?
Bob Gailer
bgailer at alum.rpi.edu
Sun Jul 27 16:55:28 EDT 2003
At 10:41 PM 7/27/2003 +0200, meinrad recheis wrote:
>i am very annoyed by the List's index out of bouds exception
>it forces me to complicate my code by adding length checking
>
>i might be spoilt by Ruby which returns nil for not existing indices.
>i want to change the List so that it returns None if the index for
>accesssing list elements
>is out of bound.
>
>i am not very experienced in python, so i ask you for suggestions.
Why is your code generating indexes that are out of range in the first place?
You can always use try - except to catch index errors in the (hopefully)
few places where there is a legitimate reason for an out of bound index.
Returning None is not a good idea, as None could be a value in the list.
But you can create a subclass of list with its own __getitem__ method that
checks bounds and returns None.
Bob Gailer
bgailer at alum.rpi.edu
303 442 2625
-------------- next part --------------
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.500 / Virus Database: 298 - Release Date: 7/10/2003
More information about the Python-list
mailing list