Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Reserved Keywords

Give Feedback

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

Maybe some commentary about this?

import keyword
print(keyword.iskeyword('keyword'))
...
-> False

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:

x = 5
print("-> x:", str(x))

str = "/usr/bin/ls"
print ("-> file:", str)
...
-> x: 5
-> file: /usr/bin/ls

In the next dev cycle, I add more code almost identical to that which works above, eg:

x = 5
print("-> x:", str(x))

str = "/usr/bin/ls"
print ("-> file:", str)

y = 5
print("-> y:", str(y))

But… BOOM!

Traceback (most recent call last):
  File ".../scratch.py", line 9, in <module>
    print("-> y:", str(y))
TypeError: 'str' object is not callable

-> x: 5
-> file: /usr/bin/ls

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?

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.