I’m trying to make life as easy as possible for my users while providing them with complete flexibility. I need to write functions for them to use, but the trick is that a user needs to pick the function before running it. Here’s what I would like to do:
def standardGenerator(obj,param=8.0):
# algorithm which generates stuff based on obj and param
return stuff
opts.usersChoice = standardGenerator
in this example, opts is an option container which is provided to the user to set whatever options they want. When they’re done setting all their options, they pass opts back to me.
now the issue is that obj will not be known until after opts is in my hands, but maybe a user wants params=4.0 instead of my default. My question is what is the easiest way to allow a user to set param?
here are the ideas I thought of:
def standardGenerator4(obj):
return standardGenerator(obj,param=4.0)
opts.usersChoice = standardGenerator4
opts.usersChoice = lambda obj: standardGenerator(obj,4.0)
probably lambda is the best idea, but i’m wondering if there’s some other way to do this without requiring users to know what a lambda function is…
Well, another way to do this is to use
functools.partial.