newbie class question

Mike Meyer mwm at mired.org
Wed Nov 23 17:48:09 EST 2005


vida00 at gmail.com writes:
> Sorry folks, this is what I meant:
>
>>>> class heh(object):
> ...     def __init__(self):
> ...         self.foo='hello'
> ...     def change(self):
> ...         self.foo+=' world'
> ...     def show(self):
> ...         return self.foo
> ...
> ...     class hih(object):
> ...         def show(self):
> ...             return heh().foo
> ...
>>>> x=heh()
>>>> print x.hih().show()
> hello
>>>> x.change()
>>>> print x.show()
> hello world
>>>> print x.hih().show()
> hello
>
> I want that last one to print 'hello world'

You create a new heh in hih.show, so it's going to get the class
variable. You need to use a class variable. Change the first four
lines of heh to :

          foo = 'hello'
          def change(self):
              heh.foo = hee.foo + ' world'

And that should do it.

    <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list