2

As far as I understand, everything in python is an object or a reference. For example: in x = 1, x is a reference to the integer object 1. 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?

if I try print type(if), unsurprisingly, I get a syntax error. I can speculate on why this is the case. Maybe if 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 if 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?

2
  • 1
    No, statements are not objects. Commented Sep 19, 2016 at 10:08
  • Statements are the recipe of your program. They're read, tokenized, parsed, and compiled to bytecode. They're not objects or functions. Commented Sep 19, 2016 at 10:09

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Right ok, that makes a lot more sense. I just tested print type(a ==b), to confirm you were right, and I had a bit of a wow moment haha when it did indeed, as you said it would, print out <type 'bool'>. Thanks a lot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.