newbie class question

vida00 at gmail.com vida00 at gmail.com
Wed Nov 23 17:18:06 EST 2005


vida00 at gmail.com wrote:
> Hi,
> I scouted the ng for someone w/ a similar problem and couldn't find
> one, so I might be thinking about this probable non-issue in a wrong
> way.
>
> What I am trying to accomplish should be pretty self explanatory when
> looking at the following:
>
> >>> 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 __init(self):
> ...             self.foo=heh.foo()
> ...         def show(self):
> ...             return self.foo
> ...
> >>> x=heh()
> >>> x.show()
> 'hello'
> >>> x.change()
> >>> x.show()
> 'hello world'
> >>> y=x.hih()
> >>> y.show()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "<stdin>", line 13, in show
> AttributeError: 'hih' object has no attribute 'foo'
>
> so, how do I reference heh.foo in its current state (i.e. 'hello
> world', not 'hello') from hih?
>
> Thanks,
>
> -Josh.

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'

Thanks, and sorry for the confusion.




More information about the Python-list mailing list