Newbie inheritance question.

bwobbones bwobbones at gmail.com
Sun Jan 16 09:08:13 EST 2005


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




More information about the Python-list mailing list