inline assignments in conditionals
Michael Gilfix
mgilfix at eecs.tufts.edu
Sat Apr 27 15:51:15 EDT 2002
On Sat, Apr 27 @ 12:33, jaf wrote:
> 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)
I'm +1 on this (Guido's gonna hate me I bet!). I recently found a
scenario (this was in C and then I thought about how I'd do it in
python) where I wanted to do an assignment only if I reached a certain
point in a bool statement, including short-circuiting. It just made
my life a lot easier because I didn't have to include redundant
case-code. B-sides, sometimes, you wish you could compress the two
statements.
-- Mike
--
Michael Gilfix
mgilfix at eecs.tufts.edu
For my gpg public key:
http://www.eecs.tufts.edu/~mgilfix/contact.html
More information about the Python-list
mailing list