suppose you have a function that can return some object or None:
def foobar(arg):
if arg == 'foo':
return None
else:
return 'bar'
Now you call this method and you want to do something with the object, for this example i get a str
, so i may want to call the upper()
function.
There are now two cases that can happen, where the second one will fail, because None has no method upper()
foobar('sdfsdgf').upper()
foobar('foo').upper()
of course this is now easy to fix:
tmp = foobar('foo')
if tmp is not None:
tmp.upper()
# or
try:
foobar('foo').upper()
except ArgumentError:
pass
# or
caller = lambda x: x.upper() if type(x) is str else None
caller(foobar('foo'))
but the exception handling is maybe not specfic enough and it can happen that i catch an exception that could be important (= debugging gets harder), while the first method is good but can result in larger code and the third option looks quite nice but you have to do it for all possible functions so method one is probably the best.
I wonder if there is any syntactic sugar that handle this problem nicely?
Since this question was answered, there are several new developments that make this pattern a bit easier.
The pymaybe package allows you to simply wrap your values with maybe
to silently propagate None
values:
from pymaybe import maybe
maybe(foobar('foo')).upper()
This is very similar to the answer by Martijn Pieters except that it preserves None
values instead of replacing them with an empty string and it'll be more robust in the presence of multiple falsey values.
Furthermore, it looks like there's a standards track draft proposal at PEP 505 that looks to add syntaxes akin to C#'s ?.
accessors to the language itself. I'm not sure how much support it has, but it's a place to look for possible further enhancements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With