def nestedclass(cls):
    '''Class decorator for making a class behave as a Java static inner class.

    Each instance of the decorated class is associated with its enclosing
    class. The outer class is referenced implicitly when an attribute lookup
    fails in the inner object's namespace. It can also be referenced
    explicitly through the attribute '__outer__' of the inner instance.
    '''
    if hasattr(cls, '__outer__'):
        raise TypeError('Cannot set attribute "__outer__" in nested class')
    class NestedDescriptor(object):
        def __get__(self, outer, outercls):
            clsdict = cls.__dict__.copy()
            # explicit read-only reference the outer class
            clsdict['__outer__'] = outercls
            # implicit lookup in the outer class
            clsdict['__getattr__'] = lambda self,attr: getattr(outercls,attr)
            def __setattr__(this, attr, value):
                # setting an attribute in the inner instance sets the
                # respective attribute in the outer class if and only if the
                # attribute is already defined in the outer class
                if hasattr(outercls, attr): setattr(outercls,attr,value)
                else: super(this.__class__,this).__setattr__(attr,value)
            clsdict['__setattr__'] = __setattr__
            return type(cls.__name__, cls.__bases__, clsdict)
    return NestedDescriptor()
Class decorator for making a class behave as a Java static inner class.

    Each instance of the decorated class is associated with its enclosing
    class. The outer class is referenced implicitly when an attribute lookup
    fails in the inner object's namespace. It can also be referenced
    explicitly through the attribute '__outer__' of the inner instance.
    
recipes/Python/409366_Implementing_Javinner_classes_using/recipe-409366.py