1

more example below:

 if (a = xyz() ) > abc: 

Really want to know why, is it kinds of bad smell ?

Note: I know the grammar, but I am asking why Python don't support such grammar as many other language( e.g., c , java...) do supporting

Jammy Lee
  • 1,496
  • 1
  • 14
  • 32

2 Answers2

4

From the python design FAQ:

The reason for not allowing assignment in Python expressions is a common, hard-to-find bug in those other languages, caused by this construct:

    if (x = 0) {
        // error handling
    }
    else {
        // code that only works for nonzero x
    }

The error is a simple typo: x = 0, which assigns 0 to the variable x, was written while the comparison x == 0 is certainly what was intended.

As explained in the FAQ, most "use cases" for using assignment in an expression can be covered by using iterators instead.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

Remember python philosophy : There should be one-- and preferably only one --obvious way to do it.
It doesn't sound obvious to me !
Also you may want to check this answer too :
why does python assignment not return a value

Community
  • 1
  • 1
Rsh
  • 7,214
  • 5
  • 36
  • 45