[Tutor] design advice for function

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Dec 18 22:19:39 CET 2005


>  > This is caused by the line: print adder().  Obviously
>  > if adder() doesn't receive any arguments, it can't
>  > build the lists resulting in an IndexError.
>
> Right.

Hello!

Just wanted to clarify the situation: argsList ends up being the empty
list, which is a perfectly good value:

######
>>> d = {}
>>> l = d.values()
>>> l
[]
######

So lists are being built perfectly ok.  The issue is that the operations
that we do on them later should account for the possibility that the lists
are empty.

One thing about the empty list, in particular, is this: because it has no
elements, it's an invalid operation to try to get the first element of an
empty list:

######
>>> l[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: list index out of range
######

Programmers will often call say something like "Don't forget the
null/empty case!", which is what this is.  *grin*


Best of wishes!




More information about the Tutor mailing list