3 ms·
How could this not happen in Python?
by hahainternet 13y ago
How could this not happen in Python?
- manojlds 13y agoBecause assigning in if would be a syntax error in Python.
- PhantomGremlin 13y agoPython does not allow an assignment to occur within an expression. They deliberately chose that restriction, to avoid that hard-to-see bug.
- staticfish 13y agoI have always wondered why all languages don't enforce this?
- joveian 13y agoBecause it is often really convenient, particularly with a simple macro preprocessor like cpp but in other circumstances too. I like gcc's approach of forcing you (if you have the right warning enabled) to put an extra pair of unnecessary parentheses around assignments in expressions.
- cnvogel 13y agobecause, for example in C, such a incompatible change would alienate most developers used to the very ideomatic if((val = function(args)) != expected) return val;
- nuxi 13y agoTrue, but in languages with operator overloading you (may) end up with other issues. Unless you look at the source you have no idea what the '==' operator will do.
- lectrick 13y agoA better solution is gcc's which forces you (if you enable the restriction) to put an extra pair of parens around inline assignments.
- dspeyer 13y agoThe parenthesis in this attack are sufficient to prevent that warning.
- nickbauman 13y agoGood question, even though Python doesn't allow assignment in an expression, but for this reason I put the constant on the left side of the comparison no matter what language I use.