Reserved Keywords
There is one more restriction on identifier names. The Python language reserves a small set of keywords that designate special language functionality. No object can have the same name as a reserved word.
In Python 3.6, there are 33 reserved keywords:
| Python Keywords |
|||
|---|---|---|---|
False |
def |
if |
raise |
None |
del |
import |
return |
True |
elif |
in |
try |
and |
else |
is |
while |
as |
except |
lambda |
with |
assert |
finally |
nonlocal |
yield |
break |
for |
not |
|
class |
from |
or |
|
continue |
global |
pass |
You can see this list any time by typing help("keywords") to the Python interpreter. Reserved words are case-sensitive and must be used exactly as shown. They are all entirely lowercase, except for False, None, and True.
John DB on Dec. 11, 2019
Ah, some investigation reveals more insight. I suggest you add a note about the difference between keywords, built-ins, and reserved words.
Here’s some StackOverflow wisdom about the topic: stackoverflow.com/questions/22864221/is-the-list-of-python-reserved-words-and-builtins-available-in-a-library
Now I realize that pylint is helpful here:
""" test """
str = "/usr/bin/ls"
print ("-> file:", str)
...
-> file: /usr/bin/ls
And here’s the desired warning:
$ pylint .../scratch.py
No config file found, using default configuration
************* Module scratch
W: 2, 0: Redefining built-in 'str' (redefined-builtin)
C: 2, 0: Constant name "str" doesn't conform to UPPER_CASE naming style (invalid-name)
-------------------------------------------------------------------
Your code has been rated at 0.00/10
Become a Member to join the conversation.

John DB on Dec. 11, 2019
Maybe some commentary about this?
It seems to me, a bigger problem with keyword clashes is not the small fixed list of 36 items above, but scenarios like this:
Suppose I have a bunch of working code, including snippets copied from StackOverflow:
In the next dev cycle, I add more code almost identical to that which works above, eg:
But… BOOM!
So… how can I find out about “almost keywords” like str which can suddenly become keywords (or keyword-like) with inadvertent use – thus turning my erstwhile “stable code” into a walking time-bomb?