import time time_prop = property(lambda self: time.time()) class Class(object): """ >>> Class.time >>> instance = Class() >>> instance.time == instance.time False """ time = time_prop def class_decorator(C): """ >>> class Class(object): ... "will magically tells the time" >>> Class = class_decorator(Class) >>> Class().time == Class().time False """ return type(C.__name__, C.__bases__, dict(C.__dict__, time=time_prop)) class TimedType(type): """ >>> TimedClass = TimedType('TimedClass', (object,), {'x': 1}) >>> TimedClass.__class__ >>> TimedType.time >>> TimedClass.time == TimedClass.time False >>> TimedClass().time Traceback (most recent call last): ... AttributeError: 'TimedClass' object has no attribute 'time' """ time = time_prop TimedClass = TimedType('TimedClass', (object,), {'x': 1}) class Enum(int): class __metaclass__(type): def __new__(metaclass, name, bases, members): Class = type.__new__(metaclass, name, bases, members) Class._names = {} for k, v in members.items(): if isinstance(v, (int, long)): setattr(Class, k, Class(v)) Class._names[v] = k return Class def __str__(self): return '%s.%s' % (self.__class__.__name__, self._names.get(self, int(self))) class FooBar(Enum): """ >>> print FooBar(0), FooBar.BAR FooBar.FOO FooBar.BAR """ FOO = 0 BAR = 1 import UserDict class Dict(object): class __metaclass__(type): # , UserDict.DictMixin __getitem__ = type.__getattribute__ __setitem__ = type.__setattr__ class A(Dict): foo = 1 class B(A): """ >>> B['bar'] 2 >>> B['foo'] 1 >>> A['foo'] = 0 >>> B['foo'] 0 """ bar = 2 # Test if __name__ == '__main__': import doctest doctest.testmod(optionflags=doctest.ELLIPSIS)