For review: PEP 308 - If-then-else expression
Ian Bicking
ianb at colorstudy.com
Fri Feb 7 22:10:38 EST 2003
On Fri, 2003-02-07 at 20:40, Andrew Dalke wrote:
> One other thing I came up with. I'll often write
>
> x = None
> if s.find("spam"):
> x = "eggs?"
>
> I do this because it guarantees that no matter what happens in the
> if statement, the 'x' will be initialized. That's handy in more complicated
> if statements. Also, it saves a line ;)
>
> Take away the newlines and replace the ": x =" with "else"
>
> x = None if s.find("spam") else "eggs?"
>
> and the behaviour is exactly opposite. I wonder how many
> times people will make that mistake?
I actually found this case to be a compelling argument for an if
expression.
x = "eggs?" if s.find("spam") else None
Taht puts "eggs?" right out in front, which is good because "eggs?" is
the interesting and useful value for x, while None is just a sort of
default value. So by switching the order I think the readability is
improved. This argument has made me think that the (true if cond else
false) form is better than (if cond then true else false) or (cond ?
true : false) form, even though initially I thought that order seemed
much more natural. I credit this change in opinion to the maturity and
new experiences I've had since I initially thought about this, oh so
many hours ago.
--
Ian Bicking ianb at colorstudy.com http://colorstudy.com
4869 N. Talman Ave., Chicago, IL 60625 / 773-275-7241
"There is no flag large enough to cover the shame of
killing innocent people" -- Howard Zinn
More information about the Python-list
mailing list