[Fixed]-Why does Django use tuples for settings and not lists?

10đź‘Ť

âś…

Based on user1474837’s helpful link to the Django ticket on this question, it seems clear that tuples are used for backwards compatibility with the way settings were done from the start, which was with tuples due to the belief they were faster than lists. (They are, but only very slightly, according to data cited in the ticket discussion.)

Specifically, Django docs used to say:

For settings that are sequences, use tuples instead of lists. This is purely for performance.

Later in the discussion, a Django core developer notes:

We’re certainly not about to move from tuples to lists there because it would break existing code that already expects things to be tuples. I’ll remove the performance note, however, since it’s not worth scaring people.

Note the word “purely” in the original documentation — which if taken at face value would mean indicating settings are immutable is not a reason tuples are used. Also note someone in the ticket discussion references settings as “sort of” immutable, so it’s not even clear settings are in fact immutable.

P.S. For interest, note the ticket resolution ends with:

Changed the “write your own settings” recommendation to mention that Django
uses tuples, but not making it a recommendation. That might head off the endless
tuples vs. lists debates.

👤Ghopper21

14đź‘Ť

This was changed in Django 1.9:

Default settings that were tuples are now lists

The default settings in django.conf.global_settings were a combination of lists and tuples. All settings that were formerly tuples are now lists.

https://docs.djangoproject.com/en/1.9/releases/1.9/#default-settings-that-were-tuples-are-now-lists

👤wannes

2đź‘Ť

I think the part of reason is tuple is read-only which will more safe and more suitable for setting.

👤lvshuchengyin

Leave a comment