keyword checker - keyword.kwlist

Sion Arrowsmith siona at chiark.greenend.org.uk
Thu May 10 11:37:07 EDT 2007


In article <_eG0i.142$Hb2.138 at read3.inet.fi>,  <tom at finland.com> wrote:
[ ... ]
>> Try to change the 'input' variable name with other...
>Changed input variable to myInput, but the result is still the same.

That was good advice, but isn't going to help here. Because "input"
isn't a keyword, it's a builtin. If you want to check builtins as
well as keywords, you need
>>> if myInput in keyword.kwlist + dir(__builtins__):
(Although obviously you'd pre-build that list.)

>for example, 'else' isn't identified as a keyword by the script though 
>it exists in keyword.kwlist.

?
>>> def check():
...     input = raw_input('Enter identifier to check >> ')
...     if input in keyword.kwlist:
...             print input, "is keyword"
...     else:
...             print input, "is not keyword"
...
Enter identifier to check >> input
input is not keyword
>>> check()
Enter identifier to check >> else
else is keyword
>>>

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
   "Frankly I have no feelings towards penguins one way or the other"
        -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list