Python Forum
Visual Studio Code - PEP8 Lambda Issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Visual Studio Code - PEP8 Lambda Issue
#1
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?
Reply
#2
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 876 Nov-15-2023, 06:56 PM
Last Post: jst
  How to set PYTHONPATH in Visual Studio Code? aupres 5 3,772 Aug-15-2023, 03:51 PM
Last Post: snippsat
  how do I open two instances of visual studio code with the same folder? SuchUmami 3 815 Jun-26-2023, 09:40 AM
Last Post: snippsat
  Updating Code And Having Issue With Keys Xileron 8 1,086 May-25-2023, 11:14 PM
Last Post: DigiGod
  Visual Studio Code NewPi 3 985 May-16-2023, 11:13 PM
Last Post: snippsat
  Visual Studio Code venv ibm_db error mesi1000 7 2,662 Nov-13-2022, 12:36 AM
Last Post: snippsat
  debugging help on Visual Studio Code mg24 12 1,900 Oct-02-2022, 12:18 AM
Last Post: mg24
  Problem with importing Python file in Visual Studio Code DXav 7 4,911 Jun-15-2022, 12:54 PM
Last Post: snippsat
  Visual Studio Code Intellisense for Imported Modules Not Working hockinsk 1 2,650 Apr-23-2022, 04:41 PM
Last Post: deanhystad
  New to python, question regarding PEP8 AthertonH 3 1,458 Apr-14-2022, 06:49 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020