Why should input(prompt="hello:") fail?

djw donald.welch.nospam at hp.com
Mon Nov 24 12:33:40 EST 2003


Peter Hansen wrote:

> zhi wrote:
>> 
>> Really confused, when I use keyword style argument as following:
>> 
>> >>> input(prompt="hello")
>> 
>> Traceback (most recent call last):
>>   File "<pyshell#52>", line 1, in -toplevel-
>>     input(prompt="hello")
>> TypeError: input() takes no keyword arguments
>> 
>> While the library reference says the function is: input( [prompt])
>> so, it should work.:(
> 
> No, it shouldn't.  The argument is not shown with a name, so you
> are supposed to use just a position argument, as in input('hello').
> 
> Note however that input() is a poor choice for serious work: you
> should quickly get past the point of wanting to use it and learn
> why raw_input() is a better choice.
> 
> -Peter

Doesn't this have more to do with the difference between C-based and Python
based modules in the std. lib? 

Discussion on this topic: 
http://tinyurl.com/wcgn

My spot checking indicates that you can use keyword args as shown in the
documentation, as long as its a Python based module. C-based modules seem
to be hit-or-miss, some support it, some don't. I actually think this could
be an improvement to the docs - actually show the way the function is
defined so that it is clear what (if any) keyword args are possible. 

The docs are ambiguous about this currently, for example, in builtins:

# Function as shown in docs:
cmp(x,y)

>>> cmp(x=1,y=2)
TypeError: cmp() takes no keyword arguments

(I would assume that cmp() has a C implementation?)

In calendar:

# Function as shown in docs:
leapdays(y1,y2)

>>> calendar.leapdays(y2=2004,y1=2003)
0

I agree that for standard, heavily used functions/methods, this is probably
OK, but it does seem rather inconsistent. Certainly for single parameter
methods like the OP asked about (input()), having a keyword adds very
little value.

-Don









More information about the Python-list mailing list