Is there any elegant way to nudge an object in front of a python tuple?

I'm using the django debug_toolbar , and I need to paste the name of the class of middleware software to other medium by default in a motorcade MIDDLEWARE_CLASSES

of settings.py

because it affects the behavior debug_toolbar.


So I want something like

MIDDLEWARE_CLASSES.push_front('debug_toolbar.middleware.DebugToolbarMiddleware')

not do

MIDDLEWARE_CLASSES = ('debug_toolbar.middleware.DebugToolbarMiddleware',) + MIDDLEWARE_CLASSES

where MIDDLEWARE_CLASSES

is a tuple.

Is there a better way to avoid writing the original tuple twice?

+3


source to share


2 answers


Tuples are immutable, so you can't do anything like mytuple.append(value)

. You always have to create a new one (or change its type for a list, then insert the value and change it back) to change the value in the variable. If your code doesn't require the use of tuples, it's much easier to use lists for that.



+4


source


If you don't need a tuple but you are ok with a list, you can use

MIDDLEWARE_CLASSES = [... whatever]
MIDDLEWARE_CLASSES.insert(0, 'debug_toolbar.middleware.DebugToolbarMiddleware')

      



Semantically, lists are meant for things like, well, lists, i.e. i.e. enumeration of things that are of the same (or similar) nature. Tuples are used to combine things, which can often happen with list items.

MIDDLEWARE_CLASSES

sounds like you would like to list some classes to be used in middleware, so even semantically a list would be better.

+2


source







All Articles