1

There is nice post on why does python assignment not return value:. But I fail to understand, why it is not an expression in python: The following is legal in Java:

           int w=5;

        while (w-- > 2){
            System.out.println(w);
        }

even here, if my understanding is correct, the assignment w-- (w=w-1) does not return anything, but sets the value for w; here assignment is followed by expression (comparison)

while w--, is not allowed in python, even assignments followed by expression is not allowed eg:

#this code does not mean anything, an assignment followed by expression pattern.

>>> while ((b=10)!=5):
  File "<stdin>", line 1
    while ((b=10)!=5):

many posts claim that typo error = vs == would lead to an assignment rather than comparison. but I fail to see this above, the assignment of b=10 happens first because it is parenthesized, then it is compared to check if it is equal to 5. There is an expression in the while statement.

Thanks for helping out

20
  • It's not clear how this question differs from the previous question. Could you clarify what you're asking that's different? Commented Dec 30, 2013 at 21:24
  • 1
    Ok, but the previous question already explains that assignments are not expressions. So clearly they can't form part of larger expressions. Commented Dec 30, 2013 at 21:28
  • 1
    What the answer in the other question you linked to is talking about is that the reason why the designers of Python decided not to make assignment an expression is because they decided it is too dangerous and would lead to too many people making errors such as typing if (a = b) instead of if (a == b) and breaking their code. Commented Dec 30, 2013 at 21:29
  • 1
    @abarnert: initialization (int a = 5) is an assignment statement in Java. java.about.com/od/understandingdatatypes/a/declaringvars.htm Commented Dec 30, 2013 at 22:23
  • 1
    @OliCharlesworth: I believe you people more than JLS. thats why I am trying to understand things clearly. our College profs, taught us differently. so thats where the confusion. anyway, Thanks for it Commented Dec 30, 2013 at 22:34

2 Answers 2

0

from a BNF grammar standpoint, an assignment is made up of an identifier being bound to an expression that reduces to a type (primitive explanation, but you get the idea), whereas an expression does not contain assignments. This is the structure of the grammar of the language. Look through the grammar and you will see that expressions cannot contain assignments.

w-- is an expression with a side-effect, so at its core it is still an expression (moot since it doesn't exist in python). Changing the state of the machine drastically in the middle of an expression evaluation can be seen as dangerous.

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

Comments

0

Since you linked to the discussion Why does Python assignment not return a value, I'll assume you understand that part.

What you're missing is a fundamental assumption that wasn't stated explicitly in the answers to that question, and which is often not stated explicitly:

Python does not have any way to embed statements into expressions.


If this were not true, there would be no point in having the statement-expression divide in the first place. That's why scripting languages that can embed statements into expressions, like JavaScript and Ruby (and, to a lesser extent, traditional C-family languages like Java and C++*), generally try to make everything possible into an expression, with some kind of useful value. There's no good reason not to do so in those languages.

Because it is true, Python gets a number of benefits from the statement-expression divide that make it easier to read, and easier to parse. For example, indentation-based block structure is much more complicated (both for the interpreter, and for the reader) if you can indirectly start a new statement in the middle of another one.

There's another property that is discussed, but which relies on some conventions in the stdlib and third-party code and in your own code. As long as nobody writes functions that both mutate state and return a useful value, you know that each statement changes exactly one thing—whether it's an assignment statement, or an expression statement with a mutating function call.** Yes, you can subvert it (e.g., you can toss a n.sort() into a list comprehension if you really want to), but unless you go out of your way to do so, this feature dramatically improves the readability of state-mutating imperative code.


Once you realize that you can never embed statements into expressions, your question becomes trivial. Since b=10 is a statement, it cannot be embedded in an expression.


* Or, really, most Algol-family languages, of which the C family are just one branch. In fact, Algol 68 found a simple way to turn everything into an expression—make a statement a kind of expression. Most other Algol derivates didn't pick that up, and over the years have rebuilt it by turning more and more kinds of statements into expressions…

** Well, obviously a, b = 2, 3 or a = b = 2 change two things—but it's still obvious what those two things are, and where to find them in the statement.

7 Comments

so what is this: a += 1, that is an assignment statement and not expression?
@user1988876: In Python? Yes, of course it is. See Augemented assignment statements. There are no assignment expressions in Python, augmented or otherwise.
That exactly right. In Java, there are assignment statements and assignment expression, when you dont have ;, it becomes expression, but there is a comment above, that the one with ; is not assignment statement but initializion which I fail to understand the difference..please see the comments right below the question
its not me who brought that, it is olicharlesworth, who brought it up. anyway, you have marked it as duplicate, which I fail to see why, since the answer you provided were not in the link the above. anyway, its not worth discussing a duplicate
@user1988876: Either (a) what you're asking about is a duplicate of the other question, or (b) you're asking something about Java and all the Python is a red herring, or (c) what you're missing is the fact that Python generally does not allow embedding statements into expressions. I'm not sure which. I wrote the answer in case it's c, and voted to close in case it's a or b. I'm hoping you'll clarify, in which case we can reopen the question if necessary, but I'm not all that hopeful at this point.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.