You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, Maybe type doesn't have special methods for being treated as bools, so any Maybe object is True in Python.
However, I find that it doesn't fit well in Python idioms. One cool feature of Optional in Python is that I can do statements like this:
ifawaitself.find_model(id):
<dosomething>
...
So I can easily check if the value is truthy or falsey. I don't need to write not is None.
However, if self.find_model would return a Maybe type, then code becomes more complex:
if (awaitself.find_model(id)) !=Nothing:
<dosomething>
...
I believe it would be a good addition to Maybe to add __bool__ method.
Couple of remarks:
I'm aware that checking whether the object is None and falsiness of a value in Python are different things. Any Python developer should be aware of this. And if anyone would return a list or strings in find_model, then that person wouldn't really be using Optional or Maybe, I think.
Refs: #874.
Currently,
Maybetype doesn't have special methods for being treated as bools, so anyMaybeobject isTruein Python.However, I find that it doesn't fit well in Python idioms. One cool feature of
Optionalin Python is that I can do statements like this:So I can easily check if the value is truthy or falsey. I don't need to write
not is None.However, if
self.find_modelwould return aMaybetype, then code becomes more complex:I believe it would be a good addition to
Maybeto add__bool__method.Couple of remarks:
Noneand falsiness of a value in Python are different things. Any Python developer should be aware of this. And if anyone would return a list or strings infind_model, then that person wouldn't really be usingOptionalorMaybe, I think.In my opinion, it's crystal clear. If I analyze a
Maybeobject then I analyzeMaybeobject, not it's value. It aligns with the documentation as well (https://returns.readthedocs.io/en/latest/pages/maybe.html#how-to-model-absence-of-value-vs-presence-of-none-value):I have one more positive argument to this semantic decision: imagine one would like to model these kinds of value:
This could be easily modeled if
__bool__would check whether aMaybeisNothingorSomething:Nothing.Something(None).Something(123).