3 ms·
Singleton is a trick to get around problems in "everything is an object" languages. It's a hack to store global data, just like the alternatives shown in this a
by kstenerud 5y ago
Singleton is a trick to get around problems in "everything is an object" languages. It's a hack to store global data, just like the alternatives shown in this article.
If you need globals and your language supports global data or functions, use that. If not, Singleton is a viable alternative.
- Traubenfuchs 5y ago> It's a hack to store global data * It's a solution to store, encapsulate and abstract global data
- humanrebar 5y agoEh. Global data doesn't really get abstracted. Abstractions over global data generally leak like a sieve.
- TheOtherHobbes 5y agoAlthough it can be hacky to implement in Python, a singleton object can enforce locking and privilege-based access control. It can also group globals into sub-objects, which can make some naming systems easier to enforce/remember. Open globals are unstructured, and more likely to turn into a messy free for all.
- nedbat 5y agoYou make it sound like "everything is an object" also means "there are no globals". What language is like that?