For example, I have an object x
that might be None
or a string representation of a float. I want to do the following:
do_stuff_with(float(x) if x else None)
Except without having to type x
twice, as with Ruby's andand library:
require 'andand'
do_stuff_with(x.andand.to_f)
We don't have one of those but it isn't hard to roll your own:
Taking off on Raymond's idea, here's a factory for making conditional wrappers of this sort. Why write 'em yourself when you can have Python write 'em for you?
andand
isn't exactly Pythonic, but I'm at a loss for a better name. Maybetrap
since you're trapping the invalid value.It's worth noting that a common Python idiom is to go ahead and try to do what you need to do, and deal with exceptions as they come along. This is called EAFP, from the maxim "it's Easier to Ask Forgiveness than Permission." So maybe a more Pythonic way to write that is:
In your use case, I think this approach is a win: it transparently deals with anything that can be converted to a
float
, and does "the right thing" if a falsy-but-convertible-to-float
value (such as 0) is passed in.