Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > newbie class question

Reply
Thread Tools

newbie class question

 
 
vida00@gmail.com
Guest
Posts: n/a
 
      11-23-2005
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.

 
Reply With Quote
 
 
 
 
vida00@gmail.com
Guest
Posts: n/a
 
      11-23-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      11-23-2005
>>What I am trying to accomplish should be pretty self explanatory when
>>looking at the following:


It seems to me that what you are after is a nested or inner class like
in JAVA. You can't do that in the same way as in JAVA, as nested classes
in python don't know about their surrounding class/context.

So, to accomplish what you want, you need e.g. this recipe from aspn:

http://aspn.activestate.com/ASPN/Coo.../Recipe/231520


Regards,

Diez
 
Reply With Quote
 
Mike Meyer
Guest
Posts: n/a
 
      11-23-2005
(E-Mail Removed) 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 <(E-Mail Removed)> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Newbie question about accessing a Variable out of a class of a class Christian Maier C++ 3 02-15-2007 08:24 AM
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Newbie Question: Function of a class keeps "forgetting" data thats declared inside class Snoeys Andy C++ 2 09-12-2003 06:15 PM



Advertisments