13

I am using Visual Studio Code and PEP8 is automatically formatting a part of my code, I was just learning about lambdas and I had a 3 line code like this:

It went from this 3 line code:

# Lambda example
divide = lambda x, y: x/y
print(divide(10, 2))

To this 7 line code:

# Lambda example


def divide(x, y): return x/y


print(divide(10, 2))

Does anyone know how do I make this program to specifically not convert my lambda function into def function?

It has been formatting my code really good, so I don't want to completely disable this automatic feature, just for the lambda thing.

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
Jsh0s
  • 519
  • 1
  • 8
  • 18
  • 1
    PEP8 is just doing it's job. See here https://stackoverflow.com/a/25010243/3508192 – charliebeckwith Jan 28 '19 at 05:48
  • 1
    The general rule of thumb is that when you're naming a lambda function, you should probably use a named one instead. – heemayl Jan 28 '19 at 05:49
  • 5
    To be clear, you defining a lambda as a variable defeats the purpose of a lambda and so pep8 is just making what you did clearer. It's error `E731`, so that'd probably be what you'd want to get rid of if you want it not to transform your code. – charliebeckwith Jan 28 '19 at 05:51
  • @charliebeckwith Thank you, do you mind explaining or linking a source that explains why "defining a lambda as a variable" is wrong? Just so I have a better understanding of it, because upon googling "Python Lambda" examples come up assigning lambda to a variable, so I'm confused – Jsh0s Jan 28 '19 at 06:06
  • I did, I think. And it's not "wrong". It's just, what you did is equivalent to defining a function except you're using lambdas in a way that are counterintuitive to their purpose. I don't wanna say antipattern, someone else will have to chime in... Here's another reference https://dzone.com/articles/pros-and-cons-lambda on why not to do that though. – charliebeckwith Jan 28 '19 at 06:11

3 Answers3

10

This is triggered by the pycodestyle code E731

You can disable this with --ignore=E731

In a config file (for instance tox.ini / setup.cfg):

[pep8] 
ignore=E731
anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • Just to be clear why - "E731 do not assign a lambda expression." – Damian Jan 28 '19 at 05:48
  • 5
    Thanks for the answer. This worked for me! Just to be absolutely clear, to add this in VSCode, search for `python formatting args` in your Settings, then add in `--ignore=E731` as an additional input argument - https://i.stack.imgur.com/FlEqM.png – rayryeng Jan 04 '21 at 21:41
5

There are some methods to disable auto converting lambda to function definition.

  • Using --ignore=E731 as explained by Anthony Sottile in (his/her) answer. Press Ctrl+,, search for autopep8, and add item --ignore=E731 as shown in the following screenshot.

    enter image description here

  • Or you uninstall autopep8 first by invoking pip uninstall autopep8 and then install yapf via pip install yapf.

  • I let others add other methods from this line.

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165
3

Another solution is to put parentheses around the lambda assignment:

divide = (lambda x, y: x/y)

autopep8 will not replace the above snippet with a def.

In general I would advise against this, as it goes against the PEP8 recommendations.

Nevertheless there are use cases for this, e.g. if the variable is conditionally assigned to different functions, which can look quite confusing when expressed using def s.

ChrisB
  • 1,540
  • 6
  • 20