Cleaning up conditionals
Deborah Swanson
python at deborahswanson.net
Sat Dec 31 16:12:06 EST 2016
> From: Tim Chase
> Sent: Saturday, December 31, 2016 12:41 PM
> On 2016-12-30 19:59, Deborah Swanson wrote:
> > Similar, actually the same as Cameron suggested. I really need to
> > revisit testing for empty. I probably rejected it early on for some
> > bad reason (you don't understand everything that goes wrong when
> > you're learning).
>
> If your data is anything like what I often get, it had spaces
> in it, and
>
> x = " "
> if x:
> print("yep")
> else:
> print("nope")
>
> prints "yep". So when processing data like this, I prefer to
> take a cleaning pass over everything to strip out
> leading/trailing whitespace before attempting to use any logic on it.
>
> -tkc
Good point. Like I said earlier, I don't remember the specific contexts
that testing for empty failed for me, but blank spaces in data that I
didn't realize would be there (or look for it) is a strong possibility.
In any event, I should use tests for empty more, and see if and why it
fails. Obviously, experienced python coders successfully use it all the
time.
More information about the Python-list
mailing list