koaning / calmcode-feedback

A repo to collect issues with calmcode.io
12 stars 0 forks source link

Improvement suggestion for the lecture on Python lambda functions #183

Open Clement-Lelievre opened 1 year ago

Clement-Lelievre commented 1 year ago

Maybe it's worth mentioning here that Python lambda functions accept keyword arguments.

Surprisingly, it isn't mentioned in the docs (at least not here)

The following code is thus valid:

func = lambda a, b = 5 : a + b
func(3) # 8
func(3,0) # 3
koaning commented 1 year ago

But it's mentioned later, here. Does that not suffice?

koaning commented 1 year ago

Ah wait! Now I see! Keyword args.

Hey yeah, that's a neet trick worth mentioning. Will keep this ticket open!

Clement-Lelievre commented 1 year ago

A possible use case is shown here

Here is a MRE:

funcs = [lambda x: x + i for i in range(3)]
X = 5
for func in funcs:
    print(func(X))

What output do you expect? Using kwargs fixes the issue