I am trying to create a function, that when imported and then called, it will check and modify a tuple. I would like to be able to call this multiple times. However, I just have the function return the new variable, because I can not figure out a way to change the variable in place.
Here is my examples with two files, how I would like it to work:
**modifier.py**
import variable
def function(new_string):
if new_string not in variable.tuple:
variable.tuple = new_string, + variable.tuple
**variable.py**
import modifier
tuple = ('one','two',)
modifier.function('add this')
modifier.function('now this')
#--> tuple should now equal ('now this', 'add this', 'one', 'two',)
However right now I have to do this:
**modifier.py**
def function(tuple_old, new_string):
if new_string not in tuple_old:
return new_string, + tuple_old
**variable.py**
import modifier
tuple = ('one','two',)
tuple = modifier.function(tuple, 'add this')
tuple = modifier.function(tuple, 'now this')
#--> tuple now equals ('now this', 'add this', 'one', 'two',)
This is a lot messier. First off, I have to pass in the old tuple value and get a returned value, instead of replacing the tuple directly. It works, but its not DRY and I know there must be a way to make this cleaner.
I can't use lists, because this is actually a function to update my middleware on my django settings file. Also I don't have to have the function on a different file, but I also think it should be possible.