class DerivedSet(set): """The simplest possible class derived from the builtin set? It's basically the same, but with a trivially extended __init__ that takes an (optional) keyword argument. DOCTESTS: (A)-(D): With no arguments, or with a single iterable argument, both set and DerivedSet yield the obvious results: >>> set() set([]) >>> DerivedSet() DerivedSet([]) >>> set([1]) set([1]) >>> DerivedSet([1]) DerivedSet([1]) Let's now exercise DerivedSet's extended functionality. First, pass in a debugString AS A POSITIONAL ARGUMENT. (E): The builtin set doesn't expect this, and fails accordingly: >>> set([1], 'print this') Traceback (most recent call last): ... TypeError: set expected at most 1 arguments, got 2 whereas (F): The DerivedSet knows to print our message: >>> DerivedSet([1], 'print this') [debugString: print this] DerivedSet([1]) Next, pass debugString in AS A KEYWORD ARGUMENT instead. (G): If your Python has had bug #1119418 fixed (i.e.: 2.4.2 or later), builtin set should complain about the unexpected keyword argument: >>> set(debugString = 'print this') ## (G): expect to fail if bug #1119418 not fixed (i.e.: version < 2.4.2c1) Traceback (most recent call last): ... TypeError: set() does not take keyword arguments [NB: Expect test (G) to fail if your Python has NOT had bug #1119418 fixed (i.e.: it's earlier than 2.4.2c1).] Finally (H): As long as they are not passed down to base classes that cannot, surely derived classes should be able to accept keyword argument(s): >>> DerivedSet(debugString = 'print this') ## (H): should pass -- but seems to fail if version >= 2.4.2c1 (presumably broken by 'fix' for bug #1119418) [debugString: print this] DerivedSet([]) Unfortunately, although debugString is clearly not passed to set, Python 2.4.2 raises the exact same exception for DerivedSet as for the builtin set. Eek! Seems like someone's been over-zealous... [NB: Expect test (H) to fail if your Python HAS had bug #1119418 'fixed' (i.e.: it's 2.4.2c1 or later)!] """ def __init__(self, initialElements = [], debugString = None): """Print debugString if provided.""" if debugString: print "[debugString: %s]" % (debugString,), set.__init__(self, initialElements) if __name__ == "__main__": import doctest doctest.testmod()