why not say just what wants an integer?

Adam Hupp hupp at cs.wisc.edu
Wed Apr 23 03:53:16 EDT 2003


On Wed, Apr 23, 2003 at 10:27:08AM +0800, 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?

How about I don't blame you for being a newbie :)

I assume you want to call range with those 3 arguments.  There are 2
ways to do this:

args = map(lambda x:x+1, (11,131,11))

range(*args)
apply(range, args)


As an aside, a nicer way of building the arguments is by using a list
comprehension:

args = [i + i  for i in (11,131,11)]

It's shorter and more readable, a rare combination.


> BTW, couldn't python be improved by saying just what wants an
  integer?

It's generally bad form to smash everything on one line like that.  A
message like "range requires an integer" would be somewhat more useful
though.


> Yes, it happened after I added range(), so range wants the integer, I
> can tell that.  So list() won't help etc.
> 
> P.S. "pydoc print" gives nothing. Should it give some clue to the user
> wanting to know about "print", or is that some violation of principles?

Sort of a violation.  Print is a keyword just like 'if' and 'def'.  so
pydoc would have to special-case all of those.  Not a bad idea but I
doubt it comes up very often.  

-Adam






More information about the Python-list mailing list