changing the List's behaviour?

Discussion in 'Python' started by meinrad recheis, Jul 27, 2003.

  1. 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
    meinrad recheis, Jul 27, 2003
    #1
    1. Advertising

  2. meinrad recheis

    Ben Finney Guest

    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/>
    Ben Finney, Jul 27, 2003
    #2
    1. Advertising

  3. meinrad recheis

    Terry Reedy Guest


    > > 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==None).

    Terry J. Reedy
    Terry Reedy, Jul 28, 2003
    #3
  4. meinrad recheis

    Peter Otten Guest

    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]
    Peter Otten, Jul 28, 2003
    #4
  5. meinrad recheis

    Peter Otten Guest

    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
    Peter Otten, Jul 30, 2003
    #5
  6. On Wed, 30 Jul 2003 17:36:12 +0200,
    Peter Otten <> 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
    Heather Coppersmith, Jul 30, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dennis Johansson
    Replies:
    1
    Views:
    499
    Dennis Johansson
    Aug 21, 2003
  2. Rogan Dawes
    Replies:
    0
    Views:
    529
    Rogan Dawes
    Jun 13, 2006
  3. Mikael Olofsson

    Changing behaviour of namespaces

    Mikael Olofsson, Sep 21, 2006, in forum: Python
    Replies:
    4
    Views:
    281
    Mikael Olofsson
    Sep 21, 2006
  4. Gernot Frisch
    Replies:
    2
    Views:
    401
    Gernot Frisch
    Feb 1, 2006
  5. Andy Chambers
    Replies:
    1
    Views:
    384
    Daniel Dyer
    May 14, 2007
Loading...

Share This Page