(Python) How do I make imported functions automatically interact with global variables?

39 views Asked by At

Let's say I want to imitate the following behaviour:

Databases = ... # 'global' object

def fn(query):
    return query_database(query, Databases)

In this case Databases is a defined object so I can call it within my function.

Now suppose instead my function 'fn' and 'Databases' are in different files, and I import 'fn' into my Databases file. Can I make it so that it replicates a similar behaviour 'out-of-the-box'? My attempt so far is the following:

def fn(query, databases = None):
    # databases are in a different file
    return query_database(query, databases)
from fn_module import fn
from toolz import curry # alternatively import partial from functools, but it gives some issues

fn = curry(fn)(databases=Databases) # Now function works as expected

Now, this works, however, I have a lot of functions, and doing this is very tedious for every function since I have to curry a lot of different variables. Is there a more systematic approach to this (or simply a total overhaul)?

0

There are 0 answers