General question about Python design goals

Christoph Zwerschke cito at online.de
Thu Dec 1 08:57:45 EST 2005


Steve Holden wrote:
> Christoph Zwerschke wrote:
>> I completely agree that his is the most frequent case. Still there are 
>> cases where tuples are used to keep homogenous values together (for 
>> instance, RGB values, points in space, rows of a matrix). In these 
>> cases it would be principally useful to have index() and count() methods.
>>
> Why? Why does it make sense to ask whether an RGB color has a particular 
> value for one of red, green or blue? Why does it make sense to ask how 
> many elements there are in an RGB color? It doesn't, so you must be 
> talking about (ordered) *collections* of such items.
 >
> If you want a list of RGB colors then use a list. If you want a list of 
> points in space then use a list. Why is a tuple preferable? [If the 
> answer is "because a tuple can't be changed" go to the bottom of the 
> class].

I cannot follow you here. How would you store RGB values? I think they 
are a perfect use case for tuples in the spirit of Guido's "lightweight 
C structs." So, in the spirit of Guido I would store them as tuples, or 
return them as tuples by a function getpixel(x,y). Why should I not be 
allowed to check for getpixel(xy).count(0) == n for black pixels in an 
image with n layers? Yes, you could set BLACK=(0,)*n and test against 
BLACK. You can always do things differently.

>> Take for example, a chess game. You are storing the pieces in a 
>> 64-tuple, where every piece has an integer value corresponding to its 
>> value in the game (white positive, black negative). You can 
>> approximate the value of a position by building the sum(). You want to 
>> use the tuple as a key for a dictionary of stored board constellations 
>> (e.g. an opening dictionary), therefore you don't use a list.
>>
> This is a pretty bogus use case. Seems to me like a special case it's 
> not worth breaking the rules for!

I already explained why use cases for count() and index() on tuples will 
principally be rare. So it will always be easy for you to call them 
"special cases". But they are there, and they make sense.

Also, we are not talking about "breaking" a rule or any existing code 
here, but about generalizing or *broadening* a rule. (What do you do if 
a rule itself is broken?)

Here is another example illustrating the problem - coincidentally, from 
Fredrik's blog, http://effbot.org/zone/image-histogram-optimization.htm
(found it when googling for getpixel):

def histogram4(image):
     # wait, use the list.count operator
     data = list(image.getdata())
     result = []
     for i in range(256):
         result.append(data.count(i))
     return result

Here, we could imagine the getdata() method returning a tuple. Again, 
why must it be casted to a list, just to use count()? In reality, 
getdata() returns a sequence. But again, wouldn't it be nice if all 
sequences provided count() and index() methods? Then there would be no 
need to create a list from the sequence before counting.

-- Christoph



More information about the Python-list mailing list