Help understanding the decisions *behind* python?

Luis Zarrabeitia kyrie at uh.cu
Fri Jul 24 13:00:38 EDT 2009


On Friday 24 July 2009 11:07:30 am Inky 788 wrote:
> On Jul 23, 3:42 am, Hendrik van Rooyen <hend... at microcorp.co.za>
> > if you think it is contrived, then please consider how you would
> > keep track of say the colour of a pixel on a screen at position
> > (x,y) - this is about the simplest "natural" tuple format and
> > example.
>
> My guess is that this is probably the way most people do it:
[...]
> def make_array_of_pixels(num_columns, num_rows):
[...]

If you need to hold /every/ pixel on the screen, it makes sense to have a 
bidimentional array of points and colors. But if you need to hold a 
non-rectangular subset, or even a subset that doesn't need to be rectangular, 
you can either use dicts, or ... do what you did. Most people will use the 
less contrived version of:

screen = {}
screen[1,2] = (rand(), rand(), rand())

(btw, in your make_a_pixel function, unless you want to make the pixels 
updatable in place, you should also be using a tuple).

Sparse datasets are extremely common, and dicts are a good way to 
[temporarily] store them.

As someone mentioned before, dictionaries are essential to python. Don't go 
out of your way to avoid them, unless you have a /reason/ to do it.

Btw,

def get_color(point):
    return screen[point]

is way more readable (and less obscure) than

def get_color(point):
    return rows_of_pixels[point[0]][point[1]]

Regards,
-- 
Luis Zarrabeitia (aka Kyrie)
Fac. de Matemática y Computación, UH.
http://profesores.matcom.uh.cu/~kyrie



More information about the Python-list mailing list