Ruby and Python

Alex Martelli aleaxit at yahoo.com
Fri Nov 17 18:39:42 EST 2000


"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:8v37c702q90 at news2.newsguy.com...
    [snip]
>     if mycombo in ds: print "yes!"
>     else: print "no..."
    [snip]
>     def __contains__(self, item):
>         if type(item) != type([]): return 0
>         if len(item)!=len(self.dice): return 0
>         for i in range(len(item)):
>             if item[i]<=0 or item[i]>self.dice[-i-1]:
>                 return 0
>         return 1

Silly me -- I need to add a guard against non-integer
elements in the 'item' list.  E.g.:

     def __contains__(self, item):
         if type(item) != type([]): return 0
         if len(item)!=len(self.dice): return 0
         for i in range(len(item)):
             if item[i] != int(item[i]): return 0
             if item[i]<=0 or item[i]>self.dice[-i-1]:
                 return 0
         return 1


Alex






More information about the Python-list mailing list