Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > changing the List's behaviour?

Reply
Thread Tools

changing the List's behaviour?

 
 
meinrad recheis
Guest
Posts: n/a
 
      07-27-2003
hi pythoneers

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.

thanks in advance,
Meinrad Recheis



 
Reply With Quote
 
 
 
 
Ben Finney
Guest
Posts: n/a
 
      07-27-2003
On Sun, 27 Jul 2003 22:41:43 +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


No, it forces you to ensure your code doesn't try to access a
non-existent list element.

Or, it forces you to catch the exception and deal with it.

> i want to change the List so that it returns None if the index for
> accesssing list elements is out of bound.


This would prevent you from distinguishing between "the index was out of
range" and "the element at that index is the None object".

> i am not very experienced in python, so i ask you for suggestions.


Take a good look at the code that is accessing non-existent index
values. Why is it doing so? Do you really want a list (ordered
collection, with a fixed set of keys) or are there cases where you want
a dict (unordered collection, arbitrary set of keys)?

--
\ "I know you believe you understood what you think I said, but I |
`\ am not sure you realize that what you heard is not what I |
_o__) meant." -- Robert J. McCloskey |
Ben Finney <http://bignose.squidly.org/>
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      07-28-2003

> > i want to change the List so that it returns None if the index for
> > accesssing list elements is out of bound.


Slicing always 'works' (does not raise exception), so seq[i:i+1]
returns empty slice rather than length 1 slice if i too large. This
is a standard idiom for getting, for instance, first element of list
if there is one when there might not be.

So slice and condition off length and if 1, extract element. This has
advantage over idea above that [] is different from [None] (which you
would get is seq[i]==None).

Terry J. Reedy


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      07-28-2003
meinrad recheis wrote:

> i want to change the List so that it returns None if the index for
> accesssing list elements
> is out of bound.


If you really need it, you can write a class similar to the one below:

class DefaultList(list):
def __init__(self, sequence=[], default=None):
list.__init__(self, sequence)
self.default = default
def __getitem__(self, index):
try:
return list.__getitem__(self, index)
except IndexError:
return self.default

if __name__ == "__main__":
theList = DefaultList("abc", "?")
print theList[1]
print theList[99]

theList = DefaultList(default="X")
print theList[1]


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      07-30-2003
Heather Coppersmith wrote:

>> class DefaultList(list):
>> def __init__(self, sequence=[], default=None):

list.init(self, sequence)
>
> That's asking for trouble. That mutable default argument for
> sequence is evaluated at class definition-time, and all instances
> of DefaultList created without a sequence argument will end up
> sharing one list.
>
> Do this instead:
>
> class DefaultList( list ):
> def __init__( self, sequence = None, default = None ):
> if sequence is None:
> sequence = [ ]
>
>> list.__init__(self, sequence)


I can see the trouble only if the sequence argument is used to initialize a
member, e.g.

def __init__(self, seq=[]):
self.seq = seq # bad, multiple instances may share one list

However, in the DefaultList case, sequence is never changed.
So I don't see what can go wrong. Am I overlooking something?

- Peter
 
Reply With Quote
 
Heather Coppersmith
Guest
Posts: n/a
 
      07-30-2003
On Wed, 30 Jul 2003 17:36:12 +0200,
Peter Otten <(E-Mail Removed)> wrote:

> Heather Coppersmith wrote:
>>> class DefaultList(list):
>>> def __init__(self, sequence=[], default=None):

> list.init(self, sequence)
>>
>> That's asking for trouble. That mutable default argument for
>> sequence is evaluated at class definition-time, and all instances
>> of DefaultList created without a sequence argument will end up
>> sharing one list.
>>
>> Do this instead:
>>
>> class DefaultList( list ):
>> def __init__( self, sequence = None, default = None ):
>> if sequence is None:
>> sequence = [ ]
>>
>>> list.__init__(self, sequence)


> I can see the trouble only if the sequence argument is used to initialize a
> member, e.g.


> def __init__(self, seq=[]):
> self.seq = seq # bad, multiple instances may share one list


> However, in the DefaultList case, sequence is never changed.
> So I don't see what can go wrong. Am I overlooking something?


> - Peter


Oops, my mistake.

Regards,
Heather

--
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing style of the numbers in a CSS <ol> without changing the style of the <li> Aaron Beall HTML 2 09-14-2007 08:07 PM
<FAQENTRY>I'm changing my page but nothing is changing on the screen. Why?</FAQENTRY> VK Javascript 3 03-15-2007 04:59 PM
Guide: Changing your Power Supply fan RObErT_RaTh Reviews & How-To's 0 08-24-2005 04:05 PM
changing file extension without changing filename jamy Computer Support 4 03-04-2005 01:03 AM
Difficulty in changing the Connection Server control path changing ... Samridhi Kumar Shukla ASP .Net 1 11-30-2003 02:31 AM



Advertisments