pythonlambdapycodestyle

Correct usage of lambda functions with Pycodestyle


I have working code with a lambda function. And it is working fine but pycodestyle informs me to no assign my lambda function to a variable. Instead I should use def to create the function. I totally understand why I should not do this, because of debugging process with millions of lambda functions as reference. Is there a way to use lambda correctly with a flake8 ruleset or are they not allowed completly for the reasons mentioned above?


Solution

  • I would not recommend you to ignore such warning. In case you really want to do that, you can just configure Flake8 to ignore the rule E731.

    Add this to your project's configuration file:

    [flake8]
    ignore = E731
    

    Lambdas in general are allowed. What flake8 recommends against is assigning a name to a lambda (anonymous) function.