python nested class

George Sakkis gsakkis at rutgers.edu
Fri Jul 8 17:40:59 EDT 2005


"Daniel Dittmar" <daniel.dittmar at sap.corp> wrote:

> Vedanta Barooah wrote:
> > in a python nested class is it possible to change the value of the
> > parent class's variable without actually creating an instance of the
> > parent class
>
> Python nested classs are like *static* Java nested classes. Non-static
> Java classes are very different in that they have an implicit reference
> to an instance of the enclosing class. This is why you can instantiate
> non-static inner classes only in the context (= non-static method) of a
> specific object. There is no direct equivalent to this in Python, so you
> have to do the steps yourself.
>
> - the constructor takes an additional argument, the 'outer' object,
> which has to be kept in the object:
> def __init__ (self, outer, ...):
>      self.outer = outer
>
> - when creating the inner object, the outer object must be passed to the
>   constructor
>      obj = InnerClass (self)
>
> - the outer object must be explicitely referenced:
>      self.outer.increase (20)
>
> Daniel

Or you can automate these steps and make implicit the reference to the
outer object using a descriptor:

#====== Test =============================================

def test():
    class Outer:
        def __init__(self,x): self.x = x

        # if python gets class decorators someday,
        # an inner class could be specified simply by:
        #@innerclass
        class Inner:
            def __init__(self, y): self.y = y
            def sum(self):  return self.x + self.y
        # as of python 2.4
        Inner = innerclass(Inner)

    outer = Outer(1)
    inner = outer.Inner(2)
    assert inner.sum() == 3
    # outer.x, inner.x, inner.__outer__.x refer to the same object
    outer.x = 4; assert inner.sum() == 6
    inner.x = 10; assert inner.sum() == 12
    inner.__outer__.x = -1; assert inner.sum() == 1
    # an inner class must be bounded to an outer class instance
    try: Outer.Inner(0)
    except AttributeError, e: pass #print e
    else: assert False

#=======================================================

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

    Each instance of the decorated class is associated with an instance
    of its enclosing class. The outer instance is referenced implicitly
    when an attribute lookup fails in the inner object's namespace. It
    can also be referenced explicitly through the property '__outer__'
    of the inner instance.
    '''
    if hasattr(cls, '__outer__'):
        raise TypeError('Existing attribute "__outer__" '
                        'in inner class')
    class InnerDescriptor(object):
        def __get__(self, outer, outercls):
            if outer is None:
                raise AttributeError('An enclosing instance that '
                                     'contains %s.%s is required' %
                                     (cls.__name__, cls.__name__))
            clsdict = cls.__dict__.copy()
            # explicit read-only reference to the outer instance
            clsdict['__outer__'] = property(lambda s: outer)
            # implicit lookup in the outer instance
            clsdict['__getattr__'] = lambda s,attr: getattr(outer,attr)
            def __setattr__(this, attr, value):
                # setting an attribute in the inner instance sets the
                # respective outer instance if and only if the
                # attribute is already defined in the outer instance
                if hasattr(outer, attr): setattr(outer,attr,value)
                else: super(this.__class__,this).__setattr__(attr,
                                                             value)
            clsdict['__setattr__'] = __setattr__
            return type(cls.__name__, cls.__bases__, clsdict)
    return InnerDescriptor()


Regards,
George




More information about the Python-list mailing list