-1

Can keywords be used as identifiers in Python? Please explain about keywords and identifiers can we use it in all workstation?

Yes or No If yes then how? If No then how?

Chris
  • 26,361
  • 5
  • 21
  • 42

1 Answers1

1

Pretty easy to find the answer to this question:

>>> class = 56
  File "<stdin>", line 1
    class = 56
          ^
SyntaxError: invalid syntax
>>> def = 42
  File "<stdin>", line 1
    def = 42
        ^
SyntaxError: invalid syntax

No, you cannot use keywords as variable names.

However, you can use the names of built-in types as variable names. It shows up on SO all the time, and it's a very bad idea because that built-in type then becomes unavailable.

E.g.

>>> from collections import defaultdict
>>> list = [42, 27]
>>> d = defaultdict(list)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: first argument must be callable or None
Chris
  • 26,361
  • 5
  • 21
  • 42
  • 1
    This isn't a complete answer; since Python 3.10, there are three "soft keywords" `match`, `case` and `_` which are permitted as identifiers in other contexts. https://docs.python.org/3/reference/lexical_analysis.html#soft-keywords – kaya3 Dec 10 '22 at 02:01