access surrounding class

Bengt Richter bokr at oz.net
Thu Dec 5 18:13:58 EST 2002


On Thu, 5 Dec 2002 22:23:14 +0100, Simon <simon.12.ghum at spamgourmet.com> wrote:

>Hello,
>
>imagine a construction like this:
>
>class theholy:
>    def dosth(self):
>        print "a grrrail??"
>        self.coconut=1231
>        self.rabbit=12
>        class snake:
>            def donothing(self):
>                print "yes, a grrrail"
>                # NOW... how can I get the value of coconut?
>                # super.self.coconut=1423: ????
>                # does not work
>        a=snake()
>        a.donothing()
>a=theholy()
>a.dosth()
>
>How can I access coconut or rabbit from within snake??

Not sure about the theology or advisablility of this for your
real problem, whatever it is, but AFAIK 'self' is just a conventional
parameter name, so to avoid shadowing just use different names:

 >>> class theholy:
 ...     def dosth(holy_self):
 ...         print "a grrrail??"
 ...         holy_self.coconut=1231
 ...         holy_self.rabbit=12
 ...         class snake:
 ...             def donothing(snake_self):
 ...                 print "yes, a grrrail"
 ...                 # NOW... how can I get the value of coconut?
 ...                 # super.self.coconut=1423: ????
 ...                 # does not work
 ...                 print 'from donothing: coconut = ',holy_self.coconut
 ...         a=snake()
 ...         a.donothing()
 ...
 >>> a=theholy()
 >>> a.dosth()
 a grrrail??
 yes, a grrrail
 from donothing: coconut =  1231

Regards,
Bengt Richter



More information about the Python-list mailing list