Antoon Pardon wrote:

> So suppose I want a dictionary, where the keys are colours, represented
> as RGB triplets of integers from 0 to 255. A number of things can be
> checked by index-like methods.
> 
> e.g.
>   
>   def iswhite(col):
>     return col.count(255) == 3
> 
>   def primary(col):
>     return col.count(255) == 1 and col.count(0) == 2
> 
>   def secondary(col):
>     return col.count(255) == 2 and col.count(0) == 1

Just because you *can* implement these by treating your colour like a list 
doesn't make it a good idea. Treating them as opaque values makes these 
particular tests clearer:

def iswhite(col):
   return col==WHITE

def primary(col):
   return col in (RED,GREEN,BLUE)

def secondary(col):
   return col in (CYAN,MAGENTA,YELLOW)

If you relax your definition of primary to simply require that two planes 
are black then you may have a case where you want to treat the colour 
planes as a list, but if you convert it explicitly then you'll be better 
placed to keep the code working when someone decides to add an alpha 
channel or to switch the representation to CMYK.

def anyshadeprimary(col):
   return [col.red, col.green, col.blue].count(0)==2
   # or
   return col.toRGB().count(0)==2

So it looks to me as though you want col to be a type (which might be a 
subclass of tuple) but a list would be a mistake.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to