[Tutor] Calling append() method in a list

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Dec 30 01:23:29 EST 2003


> Ah! In the second version of the code:
>
> ###
>     def add(self, occasion, function):
>         if self.connections.has_key(occasion) == 0:
>             self.connections[occasion].append(function)
> ###
>
> imagine what happens if self.connections is an empty dictionary.  What
> happens to the condition that's being tested by the 'if' statement?

Hi Tony,


I should be a little more explicit, just in case.  *grin*.  You tested:

###
l1=[]
l1.append(1)
l1.append(2)
l1.append(3)
###

and this is fine.  But it might be more relevant to test the situation
with the dictionary:

###
d = {}
d['names'].append('tony')
###


Hope this helps!




More information about the Tutor mailing list