Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Singleton example in Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Example of Singleton design pattern
# Copyright (C) 2011 Radek Pazdera
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
class Singleton:
# Here will be the instance stored.
__instance = None
@staticmethod
def getInstance():
""" Static access method. """
if Singleton.__instance == None:
Singleton()
return Singleton.__instance
def __init__(self):
""" Virtually private constructor. """
if Singleton.__instance != None:
raise Exception("This class is a singleton!")
else:
Singleton.__instance = self
# A little testing
s = Singleton() # Ok
#Singleton() # will raise exception
print s
s = Singleton.getInstance()
print s
s = Singleton.getInstance()
print s # will print the same instance as before
@francasix

This comment has been minimized.

Copy link

@francasix francasix commented Nov 22, 2017

Thanks, so helpful!

@nikallass

This comment has been minimized.

Copy link

@nikallass nikallass commented Feb 6, 2018

Thanks bro, after Math-like Java Python is very unstable.

@lalzada

This comment has been minimized.

Copy link

@lalzada lalzada commented Feb 19, 2018

Can you please look into this and let me know which one is the correct approach. Singleton Class in Python

@waymen

This comment has been minimized.

Copy link

@waymen waymen commented May 1, 2018

thanks, very good.

@lucrib

This comment has been minimized.

Copy link

@lucrib lucrib commented Jul 18, 2018

Super! Thank you!

@ryandgoulding

This comment has been minimized.

Copy link

@ryandgoulding ryandgoulding commented Nov 19, 2018

FYI to anyone using this in the future, this is NOT thread safe.

@codecolony

This comment has been minimized.

Copy link

@codecolony codecolony commented Dec 9, 2018

@ryandgoulding

FYI to anyone using this in the future, this is NOT thread safe.

Any solution to this?

@raushantiwari

This comment has been minimized.

Copy link

@raushantiwari raushantiwari commented Mar 1, 2019

Nice..

@ewascent

This comment has been minimized.

Copy link

@ewascent ewascent commented May 9, 2019

Well designed.

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Jun 9, 2019

== and != None should really be is and is not, but otherwise this is quite good.

@Sonic0

This comment has been minimized.

Copy link

@Sonic0 Sonic0 commented Sep 25, 2019

Thanks!!

@jnalley

This comment has been minimized.

Copy link

@jnalley jnalley commented Feb 1, 2020

I made a slight variation of this that uses the functools @lru_cache decorator instead of a class property to hold the instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment