user1367204
user1367204

Reputation: 4797

Python: How to pass parameters when it isn't an option?

I have a function - func_main - which has several inputs. One of the inputs is another function - func_mini. The smaller function, func_mini, loads some data, and it needs to be able to load data from different sources depending on the arguments passed to it. The issue is that I am reading the documentation on func_main and it says that it only accepts parameterless functions. How can I get around this?

For example:

def func_main(evaluator = None):

    num_list = [1,2,3]
    return evaluator(num_list)  



def func_mini(data_source = None):

    if not data_source:
        data_source = config.DATA_SOURCE

    return pandas.read_csv(data_source).min

I want do to this:

func_main(func_mini(data_souce='path/to/my/file'))

But func_main documentation says:

"evaluator: A parameterless function that defines and return all data needed in the above model definition."

Upvotes: 1

Views: 65

Answers (1)

timgeb
timgeb

Reputation: 78770

You can use functools.partial to build a function without parameters from your original func_mini function. Demo:

>>> from functools import partial
>>> def foo(x):
...     print(x)
... 
>>> foo_argless = partial(foo, 'hi')
>>> foo_argless()
hi

Upvotes: 6

Related Questions