monty.design_patterns module

Some common design patterns such as singleton and cached classes.

class NullFile[source]

Bases: object

A file object that is associated to /dev/null.


class NullStream[source]

Bases: object

A fake stream with a no-op write..


Does nothing… :param args:


Decorator to cache class instances by constructor arguments. This results in a class that behaves like a singleton for each set of constructor arguments, ensuring efficiency.

Note that this should be used for immutable classes only. Having a cached mutable class makes very little sense. For efficiency, avoid using this decorator for situations where there are many constructor arguments permutations.

The keywords argument dictionary is converted to a tuple because dicts are mutable; keywords themselves are strings and so are always hashable, but if any arguments (keyword or positional) are non-hashable, that set of arguments is not cached.


This decorator can be used to create a singleton out of a class.


class MySingleton():

    def __init__():