[Tutor] Dict operation question.

Kent Johnson kent37 at tds.net
Fri Jul 8 15:39:33 CEST 2005


Javier Ruere wrote:
> I should also add the most simple and obvious implementation which is also the fastest:
> 
> 
> class Test:
>     def __init__(self, dict):
>         self.somedict = dict
> 
>     def updateit(self, **mydict):
>         errdict = {}
>         for k, v in mydict.iteritems():
>             if k not in self.somedict:
>                 self.somedict[k] = v
>                 errdict[k] = v
> 
>         if (not errdict):
>             raise Exception(errdict)

My understanding of the OP is that he wants an exception if there are items in errdict. Also this solution will change self.somedict even in the error case which may not be desirable.

Kent



More information about the Tutor mailing list