Search code examples
pythonvisual-studio-codelambdajupyter-notebook

Why does Lambda work differently in VSCode and Jupyter Notebook? (python3)


Why doesn't python Lambda work in VSCode? How is VSCode different from Jupyter Notebook?

from Effective Computation in Physics book there is this Lambda example:


(lambda x, y=10: 2*x + y)(42)

Jupyter Notebook's result is:


94

Meanwhile VSCode has no output, is there some method that VSCode prefers for lambdas and why?


Solution

  • No, they run the same functions with Python. It's not really exclusive to VSCode or Jupyter at all.

    It's essentially the difference between running a compiler or an interpreter:

    In the case of VSCode, or compilers, if you want it to print something, you would have to output it with the print function. Sure, it does evaluate to 94, but it won't do anything with it.

    print((lambda x, y=10: 2*x + y)(42)) 
    

    This would suffice.

    For Jupyter, it's an interpreter. Equivalent to just running python on your command line and launching the native interpreter on your computer, it would output whatever your expression you give it evaluates to.

    As you said, it prints out 94.

    ====== Addendum:

    Technically, VSCode is an integrated development environment (IDE), and you're likely using a compiler within it.