python - Customizing a function at run-time based on parameters -
i have function passed parameters (which can vary between executions of function), not of them @ once, execute function when given rest of parameters.
the idea callback:
foo(argument) but foo customized beforehand using other parameters.
as simple example, lets take case of overlapping vs. non-overlapping text search. have function:
def textsearch(text,substring,overlapping): ... ... # stuff return index which takes text body, substring, , boolean. searches substring within body of text, using either overlapping or non-overlapping search. able create instance of function has reduced callback:
textsearch(text) by parameterizing substring , overlapping fields before callback without having create function definition. function called on multiple instances of text , operate using parameters. however, separate instance of function should able exist different parameters @ same time.
i given instance of function stored in variable called mysearch. function takes text argument , returns index of starting character of text within larger text. however, don't care if it's overlapping or non-overlapping, nor care text being searched is. i'd call:
mysearch("pancakes") with mysearch having been parameterized beforehand non-overlapping search of text "pancakes delicious.", result 0 (the index of start of text).
or maybe given different configuration searches in overlapping fashion, different text. able call:
mysearch("hash browns") the idea keep interface identical without having worry parameters define how function works or rewrite function definition. there way achieve this?
def textsearch(substring,overlapping): def parametrisedtextsearch(text): # stuff. can refer substring , overlapping here. return index return parametrisedtextsearch mysearch = textsearch(substring = whatever, overlapping = whatever) mysearch("pancakes")
Comments
Post a Comment