[SOLVED] How can I store a function with parameters as a variable without calling it immediately?

Issue

I am creating a translator for my Raspberry Pi that will get an input, then output the Morse code with an LED.

I have a function that will accurately output the Morse code for the letter of my choosing. I am trying to assign the function with parameters to a variable for quicker access like this:

a = letter(dot, dash)

but when I try and store it like that it runs the function with those parameters, I also can’t call it with something like a().

Solution

What you need is called currying, binding arguments to functions returning new functions. Or partial application. Which is the solution to your problem:

from functools import partial 
a = partial(func, arg1, arg2)

Answered By – deets

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.