Define a lambda expression that raises an Exception

后端 未结 6 652
失恋的感觉
失恋的感觉 2020-12-02 06:51

How can I write a lambda expression that\'s equivalent to:

def x():
    raise Exception()

The following is not allowed:

y =         


        
6条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 07:34

    If all you want is a lambda expression that raises an arbitrary exception, you can accomplish this with an illegal expression. For instance, lambda x: [][0] will attempt to access the first element in an empty list, which will raise an IndexError.

    PLEASE NOTE: This is a hack, not a feature. Do not use this in any (non code-golf) code that another human being might see or use.

提交回复
热议问题