summaryrefslogtreecommitdiff
path: root/poezio/singleton.py
blob: 7f12796b8aed47fc46da5ae27b2423d6b28765b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Copyright 2010-2011 Florent Le Coz <louiz@louiz.org>
#
# This file is part of Poezio.
#
# Poezio is free software: you can redistribute it and/or modify
# it under the terms of the zlib license. See the COPYING file.

"""
Defines a Singleton function that initialize an object
of the given class if it was never instantiated yet. Else, returns
the previously instantiated object.
This method is the only one that I can come up with that do not call
__init__() each time.
"""

_instances = {}
def Singleton(cls, *args, **kwargs):
    if not cls in _instances:
        _instances[cls] = cls(*args, **kwargs)
    return _instances[cls]