On inner classes

Bjorn Pettersen BPettersen at NAREX.com
Mon Feb 25 23:00:46 CET 2002


> From: Gonçalo Rodrigues [mailto:op73418 at mail.telepac.pt] 
> 
> On Mon, 25 Feb 2002 12:31:38 -0700, "Bjorn Pettersen" 
> <BPettersen at NAREX.com> wrote:
> 
> >> From: Gonçalo Rodrigues [mailto:op73418 at mail.telepac.pt]
> >> 
> >> Suppose we have something like
> >> 
> >> class <name>:
> >>     class <name>:
> >>         <whatever>
> >>     <whatever>
> >> 
> >> that is, a class defined inside another. How can the inner
> >> class acess the methods/attributes of the outer one?
> >
> >I'm assuming you're meaning something like:
> >
> >  class Outer:
> >     def outerMethod(self):
> >        class Inner:
> >           def innerMethod(this):
> >              self.outerMethod() # call class Outer's outerMethod
> >
> >-- bjorn
> 
> Actually no. Let me be more specific
> 
> class outer_class:
>     def __init__(self):
>         self.example = "just a test"
> 
>     class inner_class:
>         def __init__(self):
>             <whatever>
> 
> and now, specifically:
> 
> How in the __init__ of the inner class do i acess the 
> instance attributes declared in the __init__ of the outer 
> class? In this case the instance attribute example?

You can't. Think about it. Your inner_class is at class scope in
outer_class. What you're after is a value of an *instance* of
outer_class, however you have no handle to such an instance from within
inner_class, ergo you can't get to it.

-- bjorn




More information about the Python-list mailing list