2

Background


  • I have two files in a project. 'main.py' and 'auxillary_functions.py'. 'main.py' contains the basic code and the other file contains supporting information.
  • In the main file, I iterate over several cases. For each case, I have to initialize a large number of parameters e.g.

    for i in range(10):
    
       zP1HHSet, zH1HHSet, zISHHSet, zEEHHSet = [], [], [], []
    
       dP1HHSet, dH1HHSet, dISHHSet, dEEHHSet = [], [], [], []
    
       mP1HHSet, mH1HHSet, mISHHSet, mEEHHSet = [], [], [], []
    
       nP1HHSet, nH1HHSet, nISHHSet, nEEHHSet = [], [], [], []
    
       kP1HHSet, kH1HHSet, kISHHSet, kEEHHSet = [], [], [], []
    
       tP1HHSet, tH1HHSet, tISHHSet, tEEHHSet = [], [], [], []
    
       a_IS = 0
       b_IS = 10401
       and so on
    

This is a small subset of parameters that I need to initialize for every run.



Problem

What I want to do is to write a function, say, foo:

def foo(case):

    zP1HHSet, zH1HHSet, zISHHSet, zEEHHSet = [], [], [], []

    dP1HHSet, dH1HHSet, dISHHSet, dEEHHSet = [], [], [], []

    mP1HHSet, mH1HHSet, mISHHSet, mEEHHSet = [], [], [], []

    nP1HHSet, nH1HHSet, nISHHSet, nEEHHSet = [], [], [], []

    kP1HHSet, kH1HHSet, kISHHSet, kEEHHSet = [], [], [], []

    tP1HHSet, tH1HHSet, tISHHSet, tEEHHSet = [], [], [], []

One option known to me is to return all the variables one by one and then unpack them in the main file. Is there any better way of handling this problem?



Question

How to import a large number of variables? In short I want to reduce the main code size by transferring this group of lines to another file.



1
  • Why would you want to have so many variables? Check out libraries like pandas, which seem to be a better fit for your tabular data. Commented Dec 6, 2018 at 10:45

3 Answers 3

1

If your paramters have a structure (which seems to be the case given your example), it's a good idea to store them in a structured way instead of "flat" as a bunch of variables. This structure can then easily be returned by functions or passed as a parameter. There are a lot of different options to build structures in Python (lists, dicts, namedTuples, dataclasses, simpleNamespaces, ...). One possible solution using SimpleNamespace and NamedTuple could look like this:

from types import SimpleNamespace
from typing import NamedTuple, List

class ParamSet(NamedTuple):
    P1HHSet: List
    H1HHSet: List
    ISHHSet: List
    EEHHSet: List


def foo(case):
    return SimpleNamespace(
        z=ParamSet([], [], [], []),
        d=ParamSet([], [], [], []),
        m=ParamSet([], [], [], []),
        n=ParamSet([], [], [], []),
        k=ParamSet([], [], [], []),
        t=ParamSet([], [], [], []),
    )


if __name__ == "__main__":
    p = foo(None)

    # access members using attribute syntax
    print(p.z.P1HHSet)
    print(p.k.ISHHSet)

Please note that this will only work on a sufficiently new version of Python. The advantage of using NamedTuple and SimpleNamespace is that individual parameters can be accessed using convenient attribute syntax (e.g. p.z.P1HHSet) instead of dict (e.g. p["z"]["P1HHSet"]) or index (e.g. p[0][0]) syntax.

Does this answer your question?

Sign up to request clarification or add additional context in comments.

Comments

1

You can place all the variables in a separate file say 'auxillary_functions.py'. And in the main file, you can import all the variable using *. The file 'auxillary_functions.py' should present in the same directory as 'main.py'.

In 'main.py' you can do like this.

import * from auxillary_functions

2 Comments

How about a namedTuple ? then import this namedTuple
Dear Harish, thank you. I need to initialize these variables for each iteration. I have updated the question to illustrate the same. Thanks.
0

You could work directly on the global / local symbol table for example

globals()[ variable_name ] = variable_value
locals()[ variable_name ] = variable_value

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.