Skip to content
Advertisement

Using lambda to define a function based on another: How do I keep my code generic?

I have some python code that defines a new function based on an old one. It looks like this

JavaScript

My new function is the same as the old function, but sets the last argument to 0.

My question: Say I did not know the function took three arguments. Can I make the above solution more generic? An invalid solution with the right intention might be something like:

JavaScript

Advertisement

Answer

You’re almost correct, you can use functools.partial this way(instead of lambda):

JavaScript

Technically partial is not a function it’s a class but I don’t think this is what you concern about.

A pure Python (roughly)equivalent of the original partial exists in documentation if you want it to be a function type:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement