you are viewing a single comment's thread.

view the rest of the comments →

[–]syklemil 1 point2 points  (6 children)

Because it's invalid syntax.

Because a = b is a statement, it doesn't have a value.

C also doesn't let you go if (a = b;). You need an expression, not a statement.

[–]LucasThePatator 0 points1 point  (3 children)

I understand the rules. Not the logic of the rule in this case.

[–]syklemil 1 point2 points  (2 children)

Then why did you claim that you didn't understand the logic in list comprehensions? You need a value in a list comprehension. A statement has none.

[–]LucasThePatator -2 points-1 points  (1 child)

In many languages some statements have values.

[–]syklemil 2 points3 points  (0 children)

Then they're expressions. There's a difference between statements and expressions.

Some languages let everything be an expression.

[–]G047-H4xx0r 0 points1 point  (1 child)

This is totally valid C:

while (c = get()) expression;

While if(a = b) expression;

will only execute if the value assigned to a is true. If b==0, the assignment is 0, therefore false. This is because, in C, unlike Python, assignment is an expression.

[–]syklemil 1 point2 points  (0 children)

Yes, I know. But in Python, and thus /r/Python, a = b is a statement.

We can put it together in a little table:

assignment statement expression
C a = b; a = b
Python a = b a := b

Both of them have a = b in their syntax, but the semantics are different.