Help understanding the decisions *behind* python?
Inky 788
inky788 at gmail.com
Fri Jul 24 11:07:30 EDT 2009
On Jul 23, 3:42 am, Hendrik van Rooyen <hend... at microcorp.co.za>
wrote:
> On Wednesday 22 July 2009 16:36:51 Inky 788 wrote:
>
> > On Jul 22, 2:36 am, Hendrik van Rooyen <hend... at microcorp.co.za>
>
> > wrote:
> > > The good reason is the immutability, which lets you use
> > > a tuple as a dict key.
>
> > Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm
> > just not sophisticated enough, but I've never wanted to use a list/
> > tuple as a dict key. This sounds like obscure usage, and a bit
> > contrived as a reason for having *both* lists and tuples.
>
> 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)
~~~~
More information about the Python-list
mailing list