user7450368
user7450368

Reputation:

What is the type of 'special' operators/instructions in Python?

In Python, we have things like if, elif, else, break, continue, pass, etc. For want of a better word, I'll call them special instructions.

What type are they? If I do something like type(pass) I get a SyntaxError.

Upvotes: 1

Views: 110

Answers (2)

cs95
cs95

Reputation: 402844

According to documentation, the terminology you're looking for is "Keywords".

2.3.1. Keywords

The following identifiers are used as reserved words, or keywords of the language, and cannot be used as ordinary identifiers. They must be spelled exactly as written here:

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

These are language constructs. They are part of the language's grammar and syntax, and not objects. The interpreter treats these differently. As such, they are not associated with a type, as objects typically are.

If, for any reason, you want to know whether a word is a python keyword (aka, reserved word), you can import the keyword module and test -

import keyword

keyword.iskeyword('if')
True

Upvotes: 9

erip
erip

Reputation: 16945

They're called keywords. You can test if a string represents a keyword by using keyword.iskeyword:

>>> import keyword
>>> keyword.iskeyword("if")
True
>>> keyword.iskeyword("ifo")
False

Upvotes: 4

Related Questions