Next / Previous / Contents / Shipman's homepage

47. Test driver for Singleton

singtest
#!/usr/bin/env python
#================================================================
# Test driver for the Singleton class.
#
# Do not edit this file.  It is extracted automatically from the
# documentation:
#   logscan/
#----------------------------------------------------------------
# Output:
#----------------------------------------------------------------
# First time through, argument is 1
# it2.x is 1
#----------------------------------------------------------------

# - - - - -   I m p o r t s

from __future__ import print_function
import sys
import singleton

# - - - - -   m a i n

def main():
    """Test driver for Singleton
    """
    it1 = MyClass(1)
    it2 = MyClass(2)
    if it2 is not it1:
        print("Argh, multiple instances!")
    print("it2.x is", it2.x)

# - - - - -   c l a s s   M y C l a s s

class MyClass(singleton.Singleton):
    INITIALIZED = False

    def __init__(self, x):
        if MyClass.INITIALIZED:
            return
        else:
            MyClass.INITIALIZED = True

        print("First time through, argument is", x)
        self.x = x

# - - - - -   E p i l o g u e

if __name__ == "__main__":
    main()