How to catch exceptions using python lambdas

前端 未结 2 1475
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 10:55

Assuming Python version >=3 and calling a list of functions. I would like to write a lambda function that handles exceptions. Thing is, it does not work, when there is an ex

2条回答
  •  遥遥无期
    2021-01-14 11:04

    The short answer is that you can not really handle exceptions inside within an expression.

    A longer answer would be: you may achieve what you want by using some contrived tricks. Of course you should not do that for serious purposes, but what you can actually do inside an expression is:

    • define "on the fly" new expressions by using tricky combinations of type() and of setattr(), like this:

      MyException = (lambda o:
                        setattr(o, '__init__',
                            lambda self: setattr(self,"test", 42))
                     or setattr(o, 'my_funny_method', lambda self:True)
                     or setattr(o, 'my_other_funny_method', lambda self:False)
                     or o)(type("MyException", (BaseException,), {}))
      e = MyException()
      print(type(e), "with attribute test =", e.test)
      print(e.my_funny_method())
      raise e
      
    • raise any exception; not only by performing ad hoc operations but also in a more general manner if required:

      (_ for _ in ()).throw(ZeroDivisionError("Hello World"))
      
    • catch some exceptions by tricky uses of ad hoc features like the way StopIteration is handled by iterators:

      is_odd = lambda n: ((lambda l:
          (l and l.pop()) or "An exception was caught")
            (list((lambda: (yield from (n if n%2
               else (_ for _ in ()).throw(StopIteration) for _ in (None,))))())))
      print(is_odd(5))
      print(is_odd(8))
      

    You can read more about it at http://baruchel.github.io/python/2018/06/20/python-exceptions-in-lambda/ .

提交回复
热议问题