django middleware - Python changing modifying via imported function -
django middleware - Python changing modifying via imported function -
i trying create function, when imported , called, check , modify tuple. able phone call multiple times. however, have function homecoming new variable, because can not figure out way alter variable in place.
here examples 2 files, how 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 equal ('now this', 'add this', 'one', 'two',)
however right have this:
**modifier.py** def function(tuple_old, new_string): if new_string not in tuple_old: homecoming new_string, + tuple_old **variable.py** import modifier tuple = ('one','two',) tuple = modifier.function(tuple, 'add this') tuple = modifier.function(tuple, 'now this') #--> tuple equals ('now this', 'add this', 'one', 'two',)
this lot messier. first off, have pass in old tuple value , returned value, instead of replacing tuple directly. works, not dry , know there must way create cleaner.
i can't utilize lists, because function update middleware on django settings file. don't have have function on different file, think should possible.
i don't see wrong you're doing right (last code block), it's clear. if see like:
tuple = # ...
i know tuple changed (probably it's name used example, don't phone call variable "tuple").
but if see (what you'd do):
tuple = 'one', two' function('add this')
i'd never imagine function
changed tha value of tuple
. anyway, done with:
tuple = 'one', 'two' def function(string): global tuple if new_string not in tuple: tuple = (new_string,) + tuple function('add this')
also done:
tuple = 'one', two' function(tuple, 'add this')
i'd it's little improve because if utilize code having probelms might guess function
tuple.
and code be:
tuple = 'one', 'two' def function(old_tuple, string): global tuple if new_string not in old_tuple: tuple = (new_string,) + old_tuple function(tuple, 'add this')
at end i'd you're doing right clear , more simple, wouldn't alter it.
python django-middleware django-settings
Comments
Post a Comment