E731 do not assign a lambda expression, use a def

前端 未结 4 1156
忘掉有多难
忘掉有多难 2020-11-27 02:18

I get this pep8 warning whenever I use lambda expressions. Are lambda expressions not recommended? If not why?

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 03:04

    Lattyware is absolutely right: Basically PEP-8 wants you to avoid things like

    f = lambda x: 2 * x
    

    and instead use

    def f(x):
        return 2 * x
    

    However, as addressed in a recent bugreport (Aug 2014), statements such as the following are now compliant:

    a.f = lambda x: 2 * x
    a["f"] = lambda x: 2 * x
    

    Since my PEP-8 checker doesn't implement this correctly yet, I turned off E731 for the time being.

提交回复
热议问题