Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type is "if" in python?

Tags:

python

if [conditional]:
    program
else:
    program_alternative

How can I redefine the symbol I use for if? What if I would like to use a unicode character -- is this allowable in python? e.g. ≸ in the place of the string "if".

Is there any way to operate on this syntax? Can I say something like type(if) or perhaps type(__if__)?

I know that python permits variables to have unicode names but what if I especially want to make my code unreadable.

Or, is this something done by the parser that cannot be influenced?

like image 946
Erich Avatar asked May 24 '26 23:05

Erich


1 Answers

That is correct: "[if is ..] something done by the parser that cannot be influenced".

For clarification, if is a reserved keyword for a specific grammar construct, as defined by the Python language. In general, reserved words cannot be used as identifiers and have special parsing rules and behavior.

As such, type(if) is not even syntactically valid and will fail to parse: the program is invalid / illegal / made-up Python and the presented question of "does if have a type?" is not applicable.

We cannot use a keyword as variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language.

There is no special "if protocol" and __if__ is an undeclared identifier.

like image 147
user2864740 Avatar answered May 26 '26 13:05

user2864740