"""Simple metaclass example."""

from types import MethodType

class MetaIncrement(type):
    """A metaclass that adds an 'increment' and 'decrement' function to the
    class implementation."""
    def __init__(cls, name, bases, dct):
        """Add an 'increment' function to the class cls."""
        super(MetaIncrement, cls).__init__(name, bases, dict)
        print "instanciating metaclass..."
        print "  class : %s" % name
        print "  bases : %s" % (bases,)
        print "  dict  : %s" % dct
        setattr(cls, 'increment', lambda self, k : k+1)
        setattr(cls, 'decrement', lambda self, k : k-1)

class Test:
    """A basic class with MetaIncrement metaclass."""
    __metaclass__ = MetaIncrement
    def __init__(self):
        """Print functions."""
        print "instanciating class..."
        print "  methods:"
        for attrname in dir(self):
            if isinstance(getattr(self, attrname), MethodType):
                print "    %s" % attrname

TESTVAR = Test()
print(TESTVAR.increment(3))
print(TESTVAR.decrement(10))

