why not say just what wants an integer?

Chad Netzer cnetzer at mail.arc.nasa.gov
Wed Apr 23 09:35:39 CEST 2003


On Tue, 2003-04-22 at 19:27, Dan Jacobson wrote:
> Say, does lack of ability to figure out how to make this work 
> $ python -c 'print range(map(lambda x:x+1,(11,131,11)))'
> Traceback (most recent call last):
>   File "<string>", line 1, in ?
> TypeError: an integer is required
> 
> indicate 1. newbie, 2. lazy newbie, 3. can't really blame me?

Probably 1.

Note the following:

$ python -c 'print map(lambda x:x+1,(11,131,11))'
[12, 132, 12]


So the returned value of the map() is a list.  In older python, you
could use the apply() builtin function to unpack the list and provide
its elements as the arguments to a function.  Recent python (2.0 and
newer?) has syntax to do this:

python -c 'print range(*map(lambda x:x+1,(11,131,11)))'
[12, 24, 36, 48, 60, 72, 84, 96, 108, 120]

Note the asterisk.  In most cases, one must be specific about wanting to
treat a sequence as multiple arguments, and the asterisk notation makes
this quite convenient.

-- 

Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)






More information about the Python-list mailing list