I'm trying to eliminate a lot of repetitive code in my Python and this function repeats itself in various forms. Is there not an easier way I can just pass a variable (using a list or similar) to the function Create_me and then get it to:Create the global variable var1.Take the global variable and use it in place of var1I've tried writing some code and it just doesn't reference the correct variables. Am I trying to do something which can't be done? Please see the 2 sections of code below, the 1st one shows the function working with static variables and the 2nd shows the code I'm trying to get working. I've also done a screenshot of the whats in the memory so you see what I'm facing.Thanks in advance!def CLASS_POLICY_DEF_C350():    global var1    var1 = defaultdict(list)    worksheet = workbook.sheet_by_name('maps-C350')    headers = worksheet.row(0)    for index in range(worksheet.nrows)[1:]:        try:            for header, col in zip(headers, worksheet.row(index)):                if col.value != :                    var1[header.value].append(col.value)        except:            print sys.exc_info()def main():    CLASS_POLICY_DEF_C350()The code I've written is:#!/usr/bin/pythonfrom collections import defaultdictimport sys,xlrddef Create_me(test_var):    workbook = xlrd.open_workbook(templates.xls)    for dynamic_list in test_var:        globals()[dynamic_list] = 0        dynamic_list = defaultdict(list)        worksheet = workbook.sheet_by_name('maps-C350')        headers = worksheet.row(0)        for index in range(worksheet.nrows)[1:]:            try:                for header, col in zip(headers, worksheet.row(index)):                    if col.value != :                        dynamic_list[header.value].append(col.value)             except:                 print sys.exc_info()        print 'var1 = ',var1        print 'dynamic_list =',dynamic_list    print '\nlocal var = ',locals()    print '\nglobal var = ',globals()def main():    test_var = ('var1','var2','var3')    Create_me(test_var)if __name__ == __main__:    main()Frankus$ ./test-var.pylocal var =  {'index': 1, 'header': text:u'class-map B2', 'worksheet': , 'dynamic_list': defaultdict(, {u'class-map A1': [u'  match A2'], u'class-map B2': [u'  match B2']}), 'headers': [text:u'class-map A1', text:u'class-map B2'], 'test_var': ('var1', 'var2', 'var3'), 'workbook': , 'col': text:u'  match B2'}global var =  {'defaultdict': , 'var1': 0, 'var3': 0, 'var2': 0, 'builtins': , 'file': './test-var.py', 'package': None, 'sys': , 'Create_me': , 'name': 'main', 'xlrd': , 'main': , 'doc': None}Frankus$
Eliminate repetitive code with variables?
python