daydreamingwill daydreamingwill - 1 year ago 77
Python Question

in Python, are statements objects?

As far as I understand, everything in python is an object or a reference.
For example: in

x = 1
is a reference to the integer object
. If I write
print type(x)
, then Python will tell me the object that x is referencing is an integer.

So what about statements such as

if I try
print type(if)
, unsurprisingly, I get a syntax error. I can speculate on why this is the case. Maybe
is a static method of a class, or maybe it has somehow been weirdly defined as non returnable, etc. I just don't know.

Ultimately, I suspect that
has nothing to do with an object or a reference. However, that would surely go against the idea of everything being an object or a reference?

Answer Source

When they say "everything is an object or a reference" they are referring specifically to data. So this naturally does not apply to statements. Of course, all expressions will result in data. For example a == b is <class 'bool'> because it is an expression.

There are some languages where if is an expression but python is not one of them.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download