Singleton: Difference between revisions

m (→‎{{header|Julia}}: an older version 0.4 version syntax example got marked as incorrect, so updated.)
Line 1,690:
 
=={{header|Python}}==
===per Borg Design===
In Python we use the [http://code.activestate.com/recipes/66531/ Borg pattern] to share state between instances rather than concentrate on identity.
 
Line 1,712 ⟶ 1,713:
True
>>> # For any datum!</lang>
 
===per MetaClass/AbstractBaseClass===
 
An approximation of the singleton can be made using only class attributes to store data instead of the instance attributes, providing at least one abstract instance method (class can not be instantiated then) and making the rest of the methods being class methods. E.g.
Line 1,755 ⟶ 1,758:
<br>
So, instantiation is not possible. Only a single object is available, and it behaves as a singleton.
 
 
 
===per MetaClass===
 
 
<lang python>
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
 
class Logger(object):
__metaclass__ = Singleton
</lang>
 
or in Python3
 
 
<lang python>
class Logger(metaclass=Singleton):
pass
</lang>
 
=={{header|PureBasic}}==
Anonymous user