newbie class question

V

vida00

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:
.... 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
....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.
 
V

vida00

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:

... 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
...
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:
.... 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
....hello

I want that last one to print 'hello world'

Thanks, and sorry for the confusion.
 
D

Diez B. Roggisch

What I am trying to accomplish should be pretty self explanatory when
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/Cookbook/Python/Recipe/231520


Regards,

Diez
 
M

Mike Meyer

Sorry folks, this is what I meant:

... 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
...
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top