简体   繁体   中英

Best way to make data loaded in main() an implicit argument of a function in Python

My main function loads a largish dataframe from a csv supplied by the user (along with several other data objects), and then instantiates an object that forms a bunch of equations as part of a mathematical programming problem. Many of the equations' components are returned by calls to about 5 helper functions that I define in a utils file (most importantly, outside of the class that stores the optimization problem). These helper functions make reference to the data loaded in main, but I want their calls to show up in the equations as parameterized only by a time index t (not by the dataframe), for readability. Is the best way to accomplish this to define those functions in utils.py with the data as explicit parameters, and then use functools.partial before generating the equations to make them implicit?

This seems like a verbose approach to me, but the other options seem worse: to define the helper functions inside of main, or to give up on the idea of a main function loading the data, which basically means giving up on having a main function. And possibly having confusing circular imports.

I ended up going with my proposed answer:

define those functions in utils.py with the data as explicit parameters, and then use functools.partial before generating the equations to make them implicit

and with that codebase having been in production for over a year, it seems reasonable enough to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM