[Python-ideas] Maybe/Option builtin

mulhern mulhern at gmail.com
Wed May 21 16:49:36 CEST 2014


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.

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.

I feel that it would be kind of tricky to implement it as a class.
Something like:

class Maybe(object):

    def __init__(self, value=None):
        self.value = value

    def value(self):
        return self.value

is a start but I'm not able to see how to make

if Maybe():
    print("nothing") # never prints

but

if Maybe({}):
    print("yes a value") #always prints

which is definitely the desired behaviour.

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.

Any comments?

Thanks!

- mulhern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140521/403c60f4/attachment.html>


More information about the Python-ideas mailing list