3

We can get a list of Python keywords as follows:

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Cool, but I didn't expect to see False, None, and True there. They are builtin objects.

Why are True, False, and None keywords, but int isn't? What really makes something a keyword in Python?

Edit: I am talking about Python 3

multigoodverse
  • 7,638
  • 19
  • 64
  • 106
  • 2
    why would you expect `int` in there? If python had explicit typing, sure, maybe there'd be an `int` keyword, but in python `int()` is a function. – Zinki Feb 11 '19 at 13:36
  • 2
    The Python version matters here. – kabanus Feb 11 '19 at 13:37
  • Perhaps there is some confusion here because one might mistakenly think of keywords as "words that can't be used in an expression where an identifier would usually go". This accurately describes 99% of keywords -- for example, `2 + def` is not legal syntax. But False, None, and True _can_ be used in expressions, so they seem to break the rule. But the truth is, there is no such rule. If a keyword represents an object, then you can use it like an object. – Kevin Feb 11 '19 at 13:48

4 Answers4

8

Keywords are reserved names, so you can't assign to them.

>>> True = 0
  File "<stdin>", line 1
SyntaxError: can't assign to keyword

int is a type; it's perfectly possible to reassign it:

>>> int = str
>>>

(I really wouldn't recommend this, though.)

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

Python isn't like Javascript. In Javascript, you can do things like undefined = "defined" (update: this has been fixed).

Keywords depend on which python you use. Ex: async is a new keyword in 3.7.

Things haven't always been that way though, in Python 2 True = False was valid...

>>> True = False
>>> True
False
>>> True is False
True

So "They are builtin objects.", yes, but new versions of python prevent you from being stupid. This is the only reason why...

New keywords (since Python 2.7) are :

False
None
True
async
await
nonlocal

and of course exec and print aren't keywords anymore.

Benoît P
  • 3,179
  • 13
  • 31
1

Actually keywords are predefined, reserved names in Python that have special meaning for language's parser. Sometime they declare that we're about to define:

  1. An instance of a type (like what def and class keywords does)
  2. Condition statement (like if, while ...)
  3. ...
  4. Some times they are actual objects like True, False, None.

Because I see no body mentioned, we have two types of keywords: (Examples are from python 3.10)

1- Hard keywords: (keyword.kwlist)

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break',
 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally',
 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal',
 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

2- Soft keywords: (keyword.softkwlist)

['_', 'case', 'match']

You can not use hard keywords as a variable name or assign something to them. They are reserved names in all places. But you can have a variable called match and also you can have them inside an expression as a normal variable. match is only have special meaning if it is in the first line of a match-case block.

From PEP 634:

Remember that match and case are soft keywords, i.e. they are not reserved words in other grammatical contexts (including at the start of a line if there is no colon where expected).

S.B
  • 13,077
  • 10
  • 22
  • 49
0

in python 2.6 you could do something like True = False (really confusing)

It may help you this link

kederrac
  • 16,819
  • 6
  • 32
  • 55