Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Everything an Object in python but why not keywords as Objects?

I am new to python programming, beauty of python is Everything an Object but why not keywords as Objects?

>>> type(for)
  File "<stdin>", line 1
    type(for)
           ^
SyntaxError: invalid syntax
like image 292
user1559873 Avatar asked May 13 '26 00:05

user1559873


1 Answers

Actually, operators are objects; Have a look at the operator module. You cannot get the operator objects with their usual name (since that would significantly complicate Python's grammar, and be confusing), but this works:

>>> import operator
>>> type(operator.add)
<type 'builtin_function_or_method'>

Flow control keywords such as if and for are not objects because there is no semantic - what would you do with a hypothetical for object? Note that there are functions that can replace a for loop, namely filter, map, reduce, and a variety of functions in itertools.

like image 57
phihag Avatar answered May 14 '26 13:05

phihag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!