crimes in Python

Michael Hudson mwh21 at cam.ac.uk
Wed Mar 8 15:39:27 CET 2000


kragen at dnaco.net (Kragen Sitaker) writes:

> >> sys.stderr.write(join(
> >> 	split('crimeno crime type age sex race', ' '), "\t"))
> >> sys.stderr.write(join(
> >> 	map((lambda x: 'age' + `x` + "\tsex" 
> >> 	     + `x` + "\trace" + `x`),
> >> 	range(1, max_suspects+1)))
> >> 	)
> >
> >This looks like it could be improved a lot.
> 
> I thought so too.

Random advice: while you're learning Python, forget map exists.

This is a particularly heinous use of it, as you're useing it for
side-effects and ignoring the results.  I think map is occasionally
useful for actually mapping a function over a list of inputs, but in
this context it makes no sense.

> By the way, it's broken, now that I look at it.  It puts unwanted
> spaces in the output.
> 
> lambda x: '\tage%d\tsex%d\trace%d' % (x, x, x) would be one
> improvement.  Perhaps
> 
> for i in range(1, max_suspects+1):
> 	sys.stderr.write('\tage%d\tsex%d\trace%d' % (i, i, i))

This is so much better, isn't it?  I can tell at a glance what you're
up to, whereas I couldn't really tell from your `map'.

Cheers,
M.

-- 
very few people approach me in real life and insist on proving they are
drooling idiots.                         -- Erik Naggum, comp.lang.lisp



More information about the Python-list mailing list