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

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

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

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. Jon
    Replies:
    1
    Views:
    387
    John Saunders
    Aug 6, 2003
  2. Billy Boy

    Newbie Question on Web Form Inheritance

    Billy Boy, Mar 5, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    423
    Billy Boy
    Mar 6, 2004
  3. Ex_Ottoyuhr
    Replies:
    5
    Views:
    650
    Ex_Ottoyuhr
    Jul 22, 2004
  4. Ed Leafe

    Re: Newbie inheritance question.

    Ed Leafe, Jan 16, 2005, in forum: Python
    Replies:
    2
    Views:
    353
  5. Al
    Replies:
    2
    Views:
    472
  6. Replies:
    5
    Views:
    550
    sumedh.....
    Jul 20, 2007
  7. Replies:
    5
    Views:
    426
  8. Jerry C.
    Replies:
    8
    Views:
    586
    Uri Guttman
    Nov 23, 2003
Loading...