Help understanding the decisions *behind* python?

Hendrik van Rooyen hendrik at microcorp.co.za
Sat Jul 25 04:10:32 EDT 2009


On Friday 24 July 2009 17:07:30 Inky 788 wrote:
> On Jul 23, 3:42 am, Hendrik van Rooyen <hend... at microcorp.co.za>
>
> wrote:
8<------------------------
> > Steven showed why you cannot have a mutable thing
> > as a key in a dict.
> >
> > 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:
>
> ~~~~
> #!/usr/bin/env python
>
> import sys
> import random
>
> if len( sys.argv ) != 3:
>     print "Please pass exactly 2 ints. Exiting."
>     sys.exit(1)
>
> NUM_COLUMNS = int( sys.argv[1] )
> NUM_ROWS    = int( sys.argv[2] )
>
> print "Making array of %s columns by %s rows." % (NUM_COLUMNS,
> NUM_ROWS)
>
> def rand():
>     return int( 255 * random.random())
>
> def make_a_pixel():
>     #       red     green   blue
>     return [rand(), rand(), rand()]
>
> def make_a_row(num_columns):
>     temp_row = []
>     for i in range(num_columns):
>         temp_row.append( make_a_pixel() )
>     return temp_row
>
> def make_array_of_pixels(num_columns, num_rows):
>     rows = []
>     for i in range(num_rows):
>         rows.append( make_a_row(num_columns) )
>     return rows
>
> def show_pixels(pixel_array):
>     for row in pixel_array:
>         for pixel in row:
>             print pixel, '  ',
>         print
>
>
> rows_of_pixels = make_array_of_pixels(NUM_COLUMNS, NUM_ROWS)
>
> show_pixels(rows_of_pixels)
> ~~~~

Good grief! - Not a dictionary in sight!

I had something in mind where point was an (x,y) tuple,
and point_colours was a dict of possibly named points 
with a list of RGB values. 
(The colour can change, the point is fixed)

If I were doing it your way, I would use the Array module's Array.
But then, strictly speaking it would not be your way any more,
would it?

- Hendrik




More information about the Python-list mailing list