Ruthus99
Ruthus99

Reputation: 502

Why does my function not return these variables correctly?

I'm creating a programme at the moment that for a multitude of reasons has to keep a large number of variables as empty strings at all times.

In order to avoid repeating this code, I have created a function like below:

def reset_variables():

    other_name_used     = ''
    name_changed    = ''
    former_name     = ''
    country_of_residence    = ''
    at_address_since = ''
    previous_address = ''
    date_of_birth = ''
    city_of_birth = ''

    return  other_name_used  
    return  name_changed  
    return  former_name  
    return  country_of_residence  
    return  at_address_since
    return  previous_address
    return  date_of_birth
    return  city_of_birth

Now at different points in my code, whenver I want to reset the same variables, I simply try to call that function so they become part of my current function's namespace (although the fact that it's not working leads me to suggest that this is not how it works).

Whenever I call this function while in another function, and then try and use the variable, I'm greeted with the following traceback:

UnboundLocalError: local variable 'other_name_used' referenced before assignment

Which leads me to suggest that the variable name is just not in the current function's namespace, however surely it must be since it was returned to it upon calling the 'reset_variables' function!?

Does anybody know why this is happening, and if so, how can I achieve this?

Thanks to everyone who helps!

Upvotes: 0

Views: 26

Answers (1)

David
David

Reputation: 111

Really?

Its not so easy to reset the variables

because you can return only one.

If you want to reset you have to define the variables as global variables and than set the value in this function to the variables

Upvotes: 1

Related Questions