newbie class question

Discussion in 'Python' started by vida00@gmail.com, Nov 23, 2005.

  1. Guest

    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.
     
    , Nov 23, 2005
    #1
    1. Advertising

  2. Guest

    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.
     
    , Nov 23, 2005
    #2
    1. Advertising

  3. >>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/Cookbook/Python/Recipe/231520


    Regards,

    Diez
     
    Diez B. Roggisch, Nov 23, 2005
    #3
  4. Mike Meyer Guest

    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 <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Nov 23, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,901
    Thomas Weidenfeller
    Oct 12, 2005
  2. Snoeys Andy
    Replies:
    2
    Views:
    423
    White Wolf
    Sep 12, 2003
  3. christopher diggins
    Replies:
    16
    Views:
    775
    Pete Becker
    May 4, 2005
  4. Joseph Turian
    Replies:
    5
    Views:
    617
  5. Christian Maier
    Replies:
    3
    Views:
    506
    John Harrison
    Feb 15, 2007
Loading...

Share This Page