xendi
xendi

Reputation: 2522

Why can't we use "pass" in a ternary statement?

Here's something simple I thought I would get away with:

foo = True
print('bar') if foo else pass

Which produces:

SyntaxError: invalid syntax

Of course I can just replace pass with None and it will work. I'm just curious: Why doesn't pass work?

Upvotes: 0

Views: 367

Answers (2)

ashraful16
ashraful16

Reputation: 2782

You can do this in a line, as else did nothing, no need of else block.

foo = True
if foo : print('bar') 

Upvotes: 1

Bharel
Bharel

Reputation: 26900

pass is a statement and not an expression.

An expression can be used just about anywhere.

Most statements have their special syntax, usually on a line of their own.

For more information about the difference between the two, see this answer.

Upvotes: 1

Related Questions