multigoodverse
multigoodverse

Reputation: 8072

What really is a keyword in Python?

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

Upvotes: 3

Views: 268

Answers (4)

S.B
S.B

Reputation: 16496

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).

Upvotes: 1

kederrac
kederrac

Reputation: 17322

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

It may help you this link

Upvotes: 0

Benoît P
Benoît P

Reputation: 3265

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.

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599630

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.)

Upvotes: 8

Related Questions