```
def animalfunc():
    dog='nonkey'
    cat= [12,3,4]
    goat = cat.append(2)
    vars()[dog]=goat
    return nonkey
animalfunc()

```

Why is this flagging an error? this is a simplified version of the issue I have with a data frame on pyspark.
I assign a generated str to a dataframe.
Python knows the str exists and that dog var here exists, and vars()[dog] exists when called. In the data frame what's more confusing is it works for the first part of the loop and then randomly forgets the str I'm assigning exists. Can anyone shed some light on this?
Error: name 'nonkey' is not defined.

```
name_list=['charlie', 'grimjaw']

string= name_list[0] + '_' + name_list[1]

vars()[string] = dataframe.unionByName(dataframe2)

```

This is the higher level example of what I'm trying to do.
Name Error: name 'charlie\_grimjaw' is not defined