Say that you mean to do:
if (something == 2)
If you forget an equal sign, the resulting code can lead to subtile bugs: if (something = 2) // Not what we meant
If you are using a Yoda condition and forget an equal sign, you'll get an error right away (so that you can correct it): if (2 = something) // Error
There are other use cases, particularly related to nil values, but this should show you the gist of it.Not in Python. Assignments as anything but stand-alone (potentially chained) statements are syntax errors.
The parser will not let you write
if something = 2:
in the first place.Putting the constant left in a conditional means you will never miss an accidental = instead of ==.
That's only useful for dumb languages
>>> a = 5
>>> if a = 3: print 'ok'; else: print 'nok'
File "<stdin>", line 1
if a = 3: print 'ok'; else: print 'nok'
^
SyntaxError: invalid syntax
Python simply does not allow assignments in conditional contexts.The difference between theory and practice is smaller in theory than in practice.
I carefully weighted my words, and I rest my case.
The compilers/interpreters I know that don't do this are PHP and Perl (C/C++ will do it if you use -Wall, which you should be using anyway).
So yeah, you're pretty much right. /snark
Don't be an idiot in the first place and you won't have to do idiotic things to prevent damage from your idiocy.
Good judgement is the result of experience.
Experience is the result of bad judgement. <-- You are here