Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a PyCharm breakpoint in a Python lambda function

I want to debug the values passed to my lambda function in Python, in PyCharm.

For example, if I have the following code, I would like to be able to set a breakpoint inside the function f, and be able to see the values of a and b:

f = lambda a, b: a + b
f(1, 2)

However, if I set a breakpoint on the first line, it will only break when the lambda is being defined, not when it is called.

My PyCharm version is:

PyCharm 2018.3.3 (Professional Edition)
Build #PY-183.5153.39, built on January 10, 2019
Licensed to XXXXX
Subscription is active until October 4, 2019
For educational use only.
JRE: 1.8.0_152-release-1343-b26 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.15.0-45-generic
like image 620
Migwell Avatar asked Oct 21 '25 05:10

Migwell


1 Answers

Pycharm's breakpoints are per-line. It should break both when the lambda is defined and when it is called. If you want to break inside the lambda body only, simply put it on its own line and break on that line.

  f = lambda a, b: (
●      a + b
  )
  f(1, 2)

You can also make a breakpoint conditional on an arbitrary Python expression. Something like

'a' in locals()

should do the trick in this case, even if you leave the lambda all on one line. Right-click on the red circle for breakpoint settings.

like image 87
gilch Avatar answered Oct 22 '25 20:10

gilch