<p dir="ltr">If you care about none as a value, you specify a different default.</p>
<p dir="ltr">NO_VALUE = object()<br>
foo = bar.get("baz", NO_VALUE)<br>
if foo is NO_VALUE:<br>
    #....</p>
<div class="gmail_quote">On May 21, 2014 5:12 PM, "Devin Jeanpierre" <<a href="mailto:jeanpierreda@gmail.com">jeanpierreda@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Wed, May 21, 2014 at 4:50 PM, Steven D'Aprano <<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>> wrote:<br>
> On Wed, May 21, 2014 at 10:49:36AM -0400, mulhern wrote:<br>
>> I feel that a Maybe/Option type, analogous to the types found in Haskell or<br>
>> OCaml would actually be useful in Python. The value analogous to the None<br>
>> constructor should be Python's None.<br>
>><br>
>> Obviously, it wouldn't give the type-checking benefits that it gives in<br>
>> statically checked languages, but every use of a Maybe object as if it were<br>
>> the contained object would give an error, alerting the user to the fact<br>
>> that None is a possibility and allowing them to address the problem sooner<br>
>> rather than later.<br>
><br>
> Since this is a Python list, you shouldn't take it for granted that we<br>
> will be familiar with Haskell or Ocaml, nor expect us to go off and<br>
> study those languages well enough to understand what a Maybe object is<br>
> used for or how it will fit into Python's execution model.<br>
><br>
> I'm no expect on either of those two languages, but it seems to me that<br>
> that the only point of Maybe is to allow the static type checker to<br>
> distinguish between (for example) "this function returns a string" and<br>
> "this function returns either a string or None". Since Python doesn't do<br>
> static, compile-time type checks, I'm having difficulty in seeing what<br>
> would be the point of Maybe in Python.<br>
<br>
Python has no way to represent "I have a value, and that value is None".<br>
<br>
e.g. what is the difference between x.get('a') and x.get('b') for x ==<br>
{'a': None} ?<br>
<br>
Otherwise, agree 100% . And anyway, retrofitting this into Python<br>
can't really work.<br>
<br>
-- Devin<br>
<br>
> As I said, I'm not an expert in Haskell, but I don't think this proposal<br>
> is helpful, and certainly not helpful enough to make it a built-in or<br>
> standard part of the language. If I have missed some benefit of Maybe,<br>
> please explain how it would apply in Python terms.<br>
><br>
><br>
><br>
>> I feel that it would be kind of tricky to implement it as a class.<br>
>> Something like:<br>
>><br>
>> class Maybe(object):<br>
>>     def __init__(self, value=None):<br>
>>         self.value = value<br>
>>     def value(self):<br>
>>         return self.value<br>
>><br>
>> is a start but I'm not able to see how to make<br>
>><br>
>> if Maybe():<br>
>>     print("nothing") # never prints<br>
><br>
> In Python 2, define __nonzero__. In Python 3, define __bool__.<br>
><br>
> <a href="https://docs.python.org/2/reference/datamodel.html#object.__nonzero__" target="_blank">https://docs.python.org/2/reference/datamodel.html#object.__nonzero__</a><br>
> <a href="https://docs.python.org/3/reference/datamodel.html#object.__bool__" target="_blank">https://docs.python.org/3/reference/datamodel.html#object.__bool__</a><br>
><br>
><br>
>> but<br>
>><br>
>> if Maybe({}):<br>
>>     print("yes a value") #always prints<br>
>><br>
>> which is definitely the desired behaviour.<br>
><br>
> Not to me it isn't. This goes against the standard Python convention<br>
> that empty containers are falsey. Since Maybe({}) wraps an empty dict,<br>
> it should be considered a false value.<br>
><br>
><br>
><br>
><br>
> --<br>
> Steven<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>
_______________________________________________<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>