Python Forum

Full Version: Why do we learn singletons in python?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
my question here
I just learn singletons and what i know is the main purpose is that you can create a unique instance of a particular type

But what is the real use of it on programming like where do we use it on games, Applications? 
Can you guys give me an examples of how can i use it? 
Thanks
I use singleton-like classes in games, however i dont do anything to make them restrict the use to one object, i just dont make a second object to begin with. Im thinking stuff like the main controller of the game, or to switch between states of the game. There will only ever be one object of Control or each state, but i dont go out of my way to restrict it. I just never make a second object to begin with.
Think of the singletons in Python, like True/False and None. They are globally available unique values.

Now say you're doing survival analysis on engines. You track the time of failure, but some of them don't fail by the end of the experiment. This is called right-censored data, and is usually assigned a special value. So you might have a special value RightCensored for engines that don't fail, and make it a singleton.
(Apr-09-2017, 09:02 AM)Qubayel Wrote: [ -> ]my question here
I just learn singletons and what i know is the main purpose is that you can create a unique instance of a particular type

But what is the real use of it on programming like where do we use it on games, Applications? 
Can you guys give me an examples of how can i use it? 
Thanks 

Typical use in an application:
  • The program configuration data
  • A database connection (it the app works with one single database)
  • The "display" object