python nested class

Daniel Dittmar daniel.dittmar at sap.corp
Fri Jul 8 04:33:17 EDT 2005


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



More information about the Python-list mailing list