[Tutor] Already Initialized Object Inheritance?

WolfRage wolfrage8765 at gmail.com
Wed Jun 15 06:47:27 CEST 2011


I can not get this to behave in the manor that I would like. I am trying
to have an object refereed to as CursesApp.Screen become the already
initialized object "stdscr". To elaborate I would like it to become that
object but to also be able to define additional methods and properties,
so more along the lines of inherit from "stdscr". Is this even possible?
Well I can make it equal to that object I can not add additional methods
and properties to it? Additionally, so that I learn; where has my
thinking been too short sited? Thank you for your help.
--
Jordan

****CODE BELOW****

#!/usr/bin/python3
"""With thi method I can make the class "Screen" become "stdscr" but if
I refernce any of the new methods or properties the applications
promptly fails and notifies me that the method or property does not
exist. Another downside of this method is I can not reference
self.Screen.* or it crashes."""
import curses
class CursesApp:
    def __init__(self, stdscr):
        self.Screen(stdscr) #This is the stdscr object.
        curses.init_pair(1,curses.COLOR_BLUE,curses.COLOR_YELLOW)
        #self.Screen.bkgd(' ', curses.color_pair(1))
        #self.mainLoop()
        
    #def mainLoop(self):
        #while 1:
            #self.Screen.refresh()
            #key=self.Screen.getch()
            #if key==ord('q'): break
            
    class Screen:
        def __init__(self,stdscr):
            self=stdscr
            #self.height, self.width = self.getmaxyx() # any reference
to these crashes
            #self.offsety, self.offsetx = -self.height/2, -self.width/2
# any reference to these crashes
            #self.curx, self.cury = 1, 1 # any reference to these
crashes
            self.clear()
            self.border(0)
            while 1:
                self.refresh()
                key=self.getch()
                if key==ord('q'): break

def main():
    cursesapp = curses.wrapper(setup)

def setup(stdscr): 
    CursesApp(stdscr)
    
if __name__ == '__main__':
    main()



****CODE BELOW****

#!/usr/bin/python3
"""With this method I can make "Screen" become "stdscr" but if I
obviously can not even define any new methods or properties. But atleast
the references can be used through out the class with out crashing."""
import curses
class CursesApp:
    def __init__(self, stdscr):
        self.Screen=stdscr #This is the stdscr object.
        curses.init_pair(1,curses.COLOR_BLUE,curses.COLOR_YELLOW)
        self.Screen.bkgd(' ', curses.color_pair(1))
        self.mainLoop()
        
    def mainLoop(self):
        while 1:
            self.Screen.refresh()
            key=self.Screen.getch()
            if key==ord('q'): break

def main():
    cursesapp = curses.wrapper(setup)

def setup(stdscr): 
    CursesApp(stdscr)
    
if __name__ == '__main__':
    main()



More information about the Tutor mailing list