Skip to content

Instantly share code, notes, and snippets.

@santosh
Created July 6, 2018 15:10
Show Gist options
  • Save santosh/48af54b3bccbbdc4bd640565f55abe29 to your computer and use it in GitHub Desktop.
Save santosh/48af54b3bccbbdc4bd640565f55abe29 to your computer and use it in GitHub Desktop.
Singleton example in #Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Singleton is something I have already used in Game Development with Unity.
class MySingleton(object):
"""docstring for MySingleton"""
_instance = None
def __new__(self):
if not self._instance:
self._instance = super(MySingleton, self).__new__(self)
self.y = 10
return self._instance
x = MySingleton()
print(x.y)
x.y = 20
# z.y have the value from class we instanced before
z = MySingleton()
print(z.y)
# See https://stackoverflow.com/q/6760685/939986 for more
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment