Is there a Python equivalent for Scala's Option or Either?

前端 未结 8 1593
忘掉有多难
忘掉有多难 2021-02-05 01:26

I really enjoy using the Option and Either monads in Scala. Are there any equivalent for these things in Python? If there aren\'t, then what is the pythonic way of handling erro

8条回答
  •  我在风中等你
    2021-02-05 01:41

    In python, for an absence of value, the variable is None, so you can do it this way.

    vars = None
    
    vars = myfunction()
    
    if vars is None:
         print 'No value!'
    else:
         print 'Value!'
    

    or even just check if a value is present like this

    if vars is not None:
         print vars
    

提交回复
热议问题