<div dir="ltr">What's a specific use case? Usually a Maybe is used to model a chain of computations which might fail. You can use exceptions for that in Python:<br><br>try:<br> foo1 = bar()<br> foo2 = baz(foo1)<br>
foo3 = quux(foo2)<br>except FooException:<br> # recover<br><br><br>On occasion I've wanted to do the opposite: call a number of functions and keep the value of the first one that doesn't throw an exception. I implemented it <a href="https://gist.github.com/ahammel/ef978511dad42479fd31">like this</a><span class="sewes0zzxddipkt">. <br>
<br>That certainly doesn't need to be a built-in, though, and I'm not convinced it belongs in that standard library. It's a relatively rare use-case, and it's easy to roll-your-own if you need it.</span><span class="sewes0zzxddipkt"></span></div>
<div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, May 21, 2014 at 7:49 AM, mulhern <span dir="ltr"><<a href="mailto:mulhern@gmail.com" target="_blank">mulhern@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div><div><div><div><div>I feel that a Maybe/Option type, analogous to the types found in Haskell or OCaml would actually be useful in Python. The value analogous to the None constructor should be Python's None.<br>
<br>Obviously, it wouldn't give the type-checking benefits that it gives in statically checked languages, but every use of a Maybe object as if it were the contained object would give an error, alerting the user to the fact that None is a possibility and allowing them to address the problem sooner rather than later.<br>
<br></div>I feel that it would be kind of tricky to implement it as a class. Something like:<br><br></div><div>class Maybe(object):<br><br></div><div> def __init__(self, value=None):<br></div><div> self.value = value<br>
<br></div><div> def value(self):<br></div><div> return self.value<br><br></div><div>is a start but I'm not able to see how to make<br><br></div><div>if Maybe():<br></div><div> print("nothing") # never prints<br>
<br></div><div>but<br><br></div><div>if Maybe({}):<br></div><div> print("yes a value") #always prints<br></div><div><br></div><div>which is definitely the desired behaviour. <br></div><div><br></div>I also think that it would be the first Python type introduced solely because of its typey properties, not because it provided any actual functionality, which might be considered unpythonic.<br>
<br></div>Any comments?<br><br></div>Thanks!<span class="HOEnZb"><font color="#888888"><br><br></font></span></div><span class="HOEnZb"><font color="#888888">- mulhern<br></font></span></div>
<br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br></blockquote></div><br></div>