Cleaning up conditionals

Paul Rubin no.email at nospam.invalid
Mon Jan 2 22:33:08 EST 2017


"Deborah Swanson" <python at deborahswanson.net> writes:
> I'm still wondering if these 4 lines can be collapsed to one or two
> lines.

In the trade that's what we call a "code smell", a sign that code (even if it 
works) should probably be re-thought after taking a step back to understand 
what it is really trying to do.

What you seem to want is to take two strings, and if exactly one of them is 
empty, then change the empty one to be the same as the non-empty one. Leaving 
aside the subscripts and just calling them x and y,

   x, y = x or y, y or x

is a concise way to do it.

That's also smelly since 1) it's overly cute, and 2) it's not clear to me why 
you'd want to do this operation.  It seems like an odd thing to want so maybe 
there's some different way to solve the surrounding problem.  If you want to 
describe the actual application and context where this appears, that might get 
some helpful comments.



More information about the Python-list mailing list