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

Community
  • 1
  • 1
brain storm
  • 30,124
  • 69
  • 225
  • 393
  • It's not clear how this question differs from the previous question. Could you clarify what you're asking that's different? – Oliver Charlesworth Dec 30 '13 at 21:24
  • I am saying there is an expression here, which is the comparsion, not just assignment, in the post, it is different, there is no comparsion or assignment is done in while loops.. – brain storm Dec 30 '13 at 21:27
  • 1
    Ok, but the previous question already explains that assignments are not expressions. So clearly they can't form part of larger expressions. – Oliver Charlesworth Dec 30 '13 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. – SamYonnou Dec 30 '13 at 21:29
  • @OliCharlesworth: In Java, not all assignments are expressions. from this post, there are assignment expressions and assignment statements. http://www.dummies.com/how-to/content/assignment-statements-in-java.html – brain storm Dec 30 '13 at 21:50
  • @user1988876: From a language POV, "assignment statement" isn't a thing. `int a = ...;` isn't an assignment at all, it's an initialization. `a = ...;` is an expression statement, whose expression happens to be an assignment. – Oliver Charlesworth Dec 30 '13 at 21:53
  • is it my headache or does this make no sense – motoku Dec 30 '13 at 21:57
  • @SeanPedersen: yes, it is definetly your head ache, if you follow the comments you will understand what SO talks about. – eagertoLearn Dec 30 '13 at 21:59
  • @user1988876: In C-family languages, `int a = …;` does three separate things: it declares a variable `a` (of type `int`), it defines the declared variable (effectively meaning it provides space for it in memory), and it initializes the variable with a initial value. Meanwhile, `a = …;` is a completely different thing: It reassigns a new value to the already-declared/defined/initialized variable. Because variable declarations are statements, `int a = …;` is a statement. Assignments are not statements—`a = …` is an expression—but any expression followed by a `;` is a statement. – abarnert Dec 30 '13 at 22:04
  • (Actually, in some C-family languages, any expression by itself is a statement, and the `;`s are there to terminate or separate statements, not part of them. Also, in some, like C itself, you can define a variable without initializing it and hopefully assign to it before using it, even though you can't in Java. But the ideas are basically the same.) – abarnert Dec 30 '13 at 22:06
  • @eagertoLearn ah yes it does make sense. not everyone bothers to read about _why_ things are the way they are... a good question – motoku Dec 30 '13 at 22:07
  • @user1988876 `int a = ...;` is a bit different than `int a(...);` if you aren't considering optimization. – motoku Dec 30 '13 at 22:11
  • @abarnert in java a local variable can be declared without initialization. but it must be assigned before it's read. – ZhongYu Dec 30 '13 at 22:18
  • @zhong.j.yu: If a local variable is declared and then definitively assigned to later, that is compiled as an initialization. If a local variable is declared and not definitely assigned to anywhere, then you get a compiler error. It's not like C, where the variable is actually compiled as being declared with an indeterminate starting value. At any rate, I doubt these details are at all relevant to the OP's question, which is why I tried not to get into them. – abarnert Dec 30 '13 at 22:21
  • 1
    @abarnert: initialization (int a = 5) is an assignment statement in Java. http://java.about.com/od/understandingdatatypes/a/declaringvars.htm – brain storm Dec 30 '13 at 22:23
  • @user1988876: Some blog does not override what the reference docs say. But, even if it did, see the link of [Declaration Statement](http://java.about.com/od/d/g/declarationstatement.htm), where it clearly shows you that `int number = 10;` is a declaration statement. – abarnert Dec 30 '13 at 22:27
  • @abarnert that's unlikely, because a local var can be definitely assigned at multiple places `int i; if(condition) i=1; else i=2;` – ZhongYu Dec 30 '13 at 22:28
  • @user1988876: Anyway, after asking me to come to chat, you seem to be ignoring my replies over there, and I'm not going to retype them all over here. If you want to ask a question about the exact syntax and terminology used in the Java language specification, ask that question. Or just read the docs yourself. I fail to see how this has anything to do with your question. – abarnert Dec 30 '13 at 22:29
  • @user1988876: Declaration/initialization is *not* an "assignment statement". Take a look at the JLS if you don't believe us. – Oliver Charlesworth Dec 30 '13 at 22:32
  • 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 – brain storm Dec 30 '13 at 22:34

2 Answers2

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.

C.B.
  • 8,096
  • 5
  • 20
  • 34
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.

Community
  • 1
  • 1
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • so what is this: `a += 1`, that is an assignment statement and not expression? – brain storm Dec 30 '13 at 21:52
  • 1
    @user1988876: In Python? Yes, of course it is. See [Augemented assignment statements](http://docs.python.org/3/reference/simple_stmts.html#augmented-assignment-statements). There are no assignment expressions in Python, augmented or otherwise. – abarnert Dec 30 '13 at 21:54
  • @user1988876: In Java, on the other hand, that's an augmented assignment expression; if you slap a `;` on the end, you can turn it into a statement, just like any other expression. – abarnert Dec 30 '13 at 21:55
  • 1
    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 – brain storm Dec 30 '13 at 21:57
  • @user1988876: Declaring vs. defining vs. initializing vs. assigning in C-family languages are a whole different question from the one you asked about, and not really relevant to Python (where assignment is all there is). If you really want to understand more about Java, write a question about Java and leave the Python out of it. – abarnert Dec 30 '13 at 22:00
  • 1
    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 – brain storm Dec 30 '13 at 22:02
  • 1
    @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. – abarnert Dec 30 '13 at 22:08