I wanted to do something like this: while True: try: def fun(a, b=b, c=c): pass except NameError as ne: name = get_the_var_name(ne) locals()[name] = '' else: break What's be best way to implement the function get_the_var_name(ne) that returns the name of the variable that could not be found? Thanks in advance, Yingjie