Skip to content
Advertisement

How do I write a function to run a function and return uniquely named outer-scope variables?

I need to run one function multiple times where 3 out of 5 arguments stay the same every time. How can I write a function to just take the unique arguments as inputs and return the outputs of the inner function as unique variables?

The function I’m trying to run is part of SQLalchemy:

JavaScript

The first argument, table name, is always different and should also be the name of the variable. The last argument will be different sometimes.

My idea was to create a dictionary with table_name: schema_name and pass it to a function that loops through the dictionary and runs the Table function.

JavaScript

I guess there are two questions here. Firstly, how do I name a new variable using the string contained in another variable? Secondly, how can I return a variable from a function so that it can be called later on?

Advertisement

Answer

You generally don’t want to dynamically create variables. Use a data structure like a dictionary instead.

How can I write a function to just take the unique arguments as inputs and return the outputs of the inner function as unique variables?

You can create a function that pulls the required objects from your current scope and just returns a Table instance:

JavaScript

You can then create a dictionary of tables:

JavaScript

If you want less repetition at the cost of readability, you can use a dictionary comprehension to create your tables from an object describing the tables:

JavaScript

SQLAlchemy also has a Metadata.reflect() method that may be useful.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement