python nested class

bruno modulix onurb at xiludom.gro
Fri Jul 8 04:33:25 EDT 2005


Roland Heiber wrote:
> Vedanta Barooah wrote:
> 
>> o = mother()
>> o.show()
>> y=mother.child()
>> y.increase(20)
>> # this should print 20
>> o.show()
>>
>> ...... is it possible somehow ???
> 
> 
> Hi,
> 
> this should do what you want:
> 
> --- test.py
> class mother:
>     x=0
>     def __init__(self):
>         mother.x=1
>     def show(self):
>         print mother.x
>     class child:
>         def increase(self,num):
>             mother.x=num
> 
> o = mother()
> o.show()
> y=mother.child()
> y.increase(20)
> # this should print 20
> o.show()


> ---
>>pythonw -u "test.py"
> 1
> 20

This may *not* be what the op want...
#--- test2.py

class mother(object):
    x=0
    def __init__(self, name):
        self.name = name
        mother.x=1
    def show(self):
        print "in %s: %d" % (self.name, mother.x)
    class child(object):
        def increase(self,num):
            mother.x=num

o = mother('o')
o.show()
y=mother.child()
y.increase(20)
# this should print 20
o.show()

o2 = mother('o2')
o2.show()
y2=mother.child()
y2.increase(10)
o2.show()
o.show()


-- 
bruno desthuilliers
ruby -e "print 'onurb at xiludom.gro'.split('@').collect{|p|
p.split('.').collect{|w| w.reverse}.join('.')}.join('@')"
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list