Skip to content Skip to sidebar Skip to footer

Python : Singleton Class Object To Persist List Between Imports ? (like Django Admin Register)

I want to have dict / list to which I can add values, just like models can be added to the admin register in django ! My attempt : (package -> __init__.py) # Singleton object #

Solution 1:

If it's a singleton you're after, have a look at this old blog post. It contains a link to a well documented implementation (here).

Solution 2:

  1. Don't. If you think you need a global you don't and you should reevaluate how you are approaching the problem because 99% of the time you're doing it wrong.

  2. If you have a really good reason to do it perhaps thread_locals() will really solve the problem you're trying to solve. This allows you to set up thread level global data. Note: This is only slightly better than a true global and should in general be avoided, and it can cause you a lot of headaches.

  3. If you're looking for a cross request "global" then you most likely want to look into storing values in memcached.

Post a Comment for "Python : Singleton Class Object To Persist List Between Imports ? (like Django Admin Register)"