Newbie inheritance question.

Discussion in 'Python' started by Jeremy Bowers, Jan 16, 2005.

  1. On Sun, 16 Jan 2005 22:08:13 +0800, bwobbones wrote:

    > Hi all,
    >
    > I'm a java programmer struggling to come to terms with python - bear
    > with me!


    Christophe already identified the problem, I wanted to address another
    Javaism in your code, for your educational benefit.

    Speaking "idiomatically" (since this is all small sample code), in Python
    you'd normally code your driver as follows:

    ----------
    import two

    t = two() # or something, there's a name conflict here, too many "two"s
    t.printHello()
    t.printTestVar()
    ----------

    Things that aren't objects shouldn't be forced to be objects and I don't
    think there is any shame in having a non-OO top level. (I go on about this
    at http://www.jerf.org/writings/bowersLaw.html#4 in more detail.)
     
    Jeremy Bowers, Jan 16, 2005
    #1
    1. Advertising

  2. Jeremy Bowers

    bwobbones Guest

    Hi all,

    I'm a java programmer struggling to come to terms with python - bear
    with me!

    I'm trying to subclass a class, and I want to be able to see it's
    attributes also. Here are my classes:

    one.py:
    *****************************
    class one:
    def __init__(self):
    print "one"
    self.testVar = 1

    def printHello(self):
    print "hello"
    *****************************

    two.py
    *****************************
    from one import one

    class two(one):
    def __init__(self):
    print "two"

    def printTestVar(self):
    print "testVar: " + str(self.testVar)
    *****************************

    and the driver to make it work:
    *****************************
    from two import two

    class driver:
    def go(self):
    print "driver"
    self.two = two()
    self.two.printHello()
    self.two.printTestVar()

    if __name__ == '__main__':
    d = driver()
    d.go()
    *****************************

    the "self.two.printTestVar()" call doesn't work: shouldn't two.py have
    access to all of it's parents attributes? In java I would make the
    testVar protected so it's children could see it. How do I do this in
    python?

    Thanks,

    Bones
     
    bwobbones, Jan 16, 2005
    #2
    1. Advertising

  3. bwobbones wrote:

    > Hi all,
    >
    > I'm a java programmer struggling to come to terms with python - bear
    > with me!
    >
    > I'm trying to subclass a class, and I want to be able to see it's
    > attributes also. Here are my classes:
    >
    > two.py
    > *****************************
    > from one import one
    >
    > class two(one):
    > def __init__(self):
    > print "two"
    >
    > def printTestVar(self):
    > print "testVar: " + str(self.testVar)
    > *****************************


    You simply forgot to call the parent __init__. You have to do that
    explicitely in Python.
     
    Christophe Cavalaria, Jan 16, 2005
    #3
    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. maxw_cc
    Replies:
    1
    Views:
    3,148
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    827
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,041
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,908
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    777
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page