0

I have a function with an unspecified number of input strings which are intended to be variable names. For example

def f(*args):
    for arg in args:
        exec('{} = 1'.format(arg))

    return a

f('a', 'b')

When running the code, I recieve the following error

NameError: name 'a' is not defined

How do I assign local variables for the function which are to be manipulated or returned? The solution provided in this similar but different question requires creating the variables outside the function, i.e. adding them to the global namespace, but that is not what I want.

Tian
  • 870
  • 3
  • 12
  • 24

0 Answers0