Newbie inheritance question.
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 -- http://mail.python.org/mailman/listinfo/python-list
usage of __import__ across two files
Hi, I'm having trouble making __import__ work with the two classes attached. The PrintHello() method can't be seen in the BMTest2 class - what am I doing wrong here? class one - BMTest - in BMTest.py: import wx from traceback import print_exc class ImportTest(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, "ImportTest", size = (666,480), style = wx.DEFAULT_FRAME_STYLE) #tb = BMToolBar(self) # works just fine! tb = __import__('BMTest2') tb2.PrintHello() class MyApp(wx.App): def __init__(self, flag): wx.App.__init__(self, flag) def OnInit(self): frame = ImportTest() self.SetTopWindow(frame) return True if __name__ == '__main__': try: app = MyApp(False) app.MainLoop() except: print print_exc() ** class 2 BMTest2 - in BMTest2.py: ** import wx class BMToolBar(wx.ToolBar): def __init__(self, parentFrame): wx.ToolBar.__init__(self, parentFrame, -1, style=wx.TB_HORIZONTAL|wx.NO_BORDER|wx.TB_FLAT|wx.TB_TEXT) print "*** gday ***" self.Realize() def PrintHello(self): print "Hello" Any help will be much appreciated! Bones -- http://mail.python.org/mailman/listinfo/python-list
re: usage of __import__ across two files
Oops, Thanks for the reply, but lets try again - I was in a real rush last night...and I obviously posted the wrong code. I'm trying to use the __import__ form of import because I want to dynamically load classes from their .py files. When I run the BMTest.py file, I get the error: File "C:\development\BMTest.py", line 10, in __init__ tb.PrintHello() AttributeError: 'module' object has no attribute 'PrintHello' It's not recognising that BMTest2 has a PrintHello method! The shell dir function reports this: import BMTest2 dir(BMTest2) ['BMToolBar', '__builtins__', '__doc__', '__file__', '__name__', 'wx'] tb = __import__('BMTest2') dir(tb) ['BMToolBar', '__builtins__', '__doc__', '__file__', '__name__', 'wx'] Am I not defining PrintHello correctly? Heres the source again (the proper version this time): ** class one - BMTest - in BMTest.py: ** import wx from traceback import print_exc #from BMTest2 import BMToolbar class ImportTest(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, "ImportTest", size = (666,480), style = wx.DEFAULT_FRAME_STYLE) #tb = BMToolBar(self) # works just fine! tb = __import__('BMTest2') tb.PrintHello() class MyApp(wx.App): def __init__(self, flag): wx.App.__init__(self, flag) def OnInit(self): frame = ImportTest() self.SetTopWindow(frame) return True if __name__ == '__main__': try: app = MyApp(False) app.MainLoop() except: print print_exc() ** class two - BMToolBar - in BMTest2.py: ** import wx class BMToolBar(wx.ToolBar): def __init__(self, parentFrame): wx.ToolBar.__init__(self, parentFrame, -1, style=wx.TB_HORIZONTAL|wx.NO_BORDER|wx.TB_FLAT|wx.TB_TEXT) print "*** gday ***" self.Realize() def PrintHello(self): print "Hello" Sorry for the confusion... Bones / / -- http://mail.python.org/mailman/listinfo/python-list
re: usage of __import__ across two files
Thanks Diez, Obvious when you put it that way... -- http://mail.python.org/mailman/listinfo/python-list