Python Forum

Full Version: Visual Studio Code - PEP8 Lambda Issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
PEP8 is automatically formatting a part of my code, I was just learning about lambdas and I had a 2 line code like this:

My simple code: (2 lines)

What PEP 8 does: (4 lines)

it may not seem like a lot, but it went from 2 lines to 4 lines, with the huge possibility of it being a 6 line code (if I had anything above the def function, it would add another 2 lines between them.)

If the def had anything above: (6 lines)

So it can easily go from 2 lines to 6 lines!

How do I disable this function specifically for lambdas?
It's not PEP8 that format your code, but the linter extension you have installed (there are several, so it's not clear which one you use - probably the one with same name?). The purpose of linter is to make your code conform with PEP8 recommendations. How strict it will be depends on your particular settings - this is to answer the question how you can disable.
Now, in this particular case PEP8 has clear recommendation:
Quote:Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier.

Yes:

def f(x): return 2*x

No:

f = lambda x: 2*x

The first form means that the name of the resulting function object is specifically 'f' instead of the generic '<lambda>'. This is more useful for tracebacks and string representations in general. The use of the assignment statement eliminates the sole benefit a lambda expression can offer over an explicit def statement (i.e. that it can be embedded inside a larger expression)