inline assignments in conditionals

jaf jaf at lcsaudio.com
Sat Apr 27 15:33:47 EDT 2002


Roman Neuhauser wrote:

>Anyone who writes "if (x == 0)" instead of "if (0 == x)" deserves what

> he begs for.

Writing things that way is really unnatural, at least for English
speakers.  It makes the intent of the code harder to understand (at
least for me).  For example, if you were talking to a friend, you
woudn't say:

   "If today is Sunday, I'm going to go shopping"

but rather:

   "If Sunday is today, I'm going to go shopping"

and in any case, writing the constant first only protects in the case
where one of the compared values is a constant.  If you are doing

   if (a == b):

then there is no way to protect yourself against actually writing '='.

Given that, I think not supportig inline assignment is a reasonable
choice.  Perhaps another option would be to coin a synonymous operator
that works in comparisons, but is distinct enough from '==' so as not
to be used by accident?

   if (data gets socket.recv(1024)):
      process(data)

-Jeremy






More information about the Python-list mailing list