Search code examples
pythonlambda

Python Vscode Lambda issue


Vscode has an issue with the lambda expression when I create a function, it works until I either save the script or run the script then the code below get changed from this automatically

  a = lambda user : user + 10
  re = a(40)
  print(re)

to this

 def a(user): return user + 10
    re = a(40)
    print(re)

Solution

  • This is not a Vscode issue. It is due to pep8 auto formatting.(vscode is suggesting you the best way to write your code) Try to disable auto format in vscode. Go to File -> Preferences -> settings Look for python> Formatting autopep8 path and turn it to null or something. But I don't recommend to do that because according to pep8 :

    "Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifie"
    

    edit : PEP8 Documentation