[Tutor] Tutor Digest, Vol 88, Issue 56

WolfRage wolfrage8765 at gmail.com
Thu Jun 16 04:20:39 CEST 2011


> From: Steven D'Aprano <steve at pearwood.info>
> To: Python Tutor <tutor at python.org>
> Subject: Re: [Tutor] Already Initialized Object Inheritance?
> Message-ID: <4DF898E9.5050104 at pearwood.info>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 
> WolfRage wrote:
> > Unfortunately I am not able to inherit "stdscr" using that method. As
> > Python returns with an error stating that "stdscr" is not defined. This
> > error is returned at run time and by the compiler prior to actual
> > execution. If you would like I can write a quick example that will
> > generate the error message for that method.
> 
> You shouldn't need to ask that question, just do it.

Ahh... sorry?

****CODE BELOW****
#!/usr/bin/python3
"""With this 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 attribute does not exist."""
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
            self.Screen.height=10 #This is the line that crashes,
because it is not an attribute of "stdscr".

    class Screen:
        def __init__(self):
            self.height, self.width = self.getmaxyx()
            self.offsety, self.offsetx = -self.height/2, -self.width/2
            self.curx, self.cury = 1, 1

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

def setup(stdscr):
    CursesApp(stdscr)

if __name__ == '__main__':
    main()
****CODE ABOVE****
> 
> My guess is that the error you get is a NameError. The solution to that 
> is to fix the name error, not to run off chasing wild geese. Object 
> inheritance? This has nothing to do with that!

Hmm... You seem to think that I should know this automatically, but
obviously I do not. Did I write the correct mailing-list? Is this not a
place to learn? Exclamation points are very similar to all caps.

The error is actually an attribute does not exist error, output as
requested is further down in the email. 

Although you would think I would cause a name collision, but Python
ignores the class defined as Screen; even if I initialize it, because it
is already defined. So this does not cause a NameError, but it does
cause a TypeError, because "self.Screen" is a "curses.curses window"
which is not a callable object. The code demonstrating this error is
also listed below at the end of the email.

> What is stdscr? Where does it come from? How is it defined?

"stdscr" is a Curses window object. Window objects in Curses are usually
created with the "curses.newwin()" function. However "stdscr" is special
in that it is created when "curses.initscr()" is run. "curses.initscr()"
creates a window object and returns it, "stdscr", that takes up the
available space in the terminal or screen. 
"curses.initscr()" is not present in the code because it is executed by
"curses.wrapper()", as part of the terminal safe initialization process
for Curses.  Hopefully this answers your three questions
satisfactorily. 
Reference: http://docs.python.org/library/curses.html 
> 
> Please copy and paste the *exact* error message you get, including the 
> full traceback.
> 
****CODE/ERROR OUTPUT BELOW****
$ python3 quick_test.py 
Traceback (most recent call last):
  File "quick_test.py", line 31, in <module>
    main()
  File "quick_test.py", line 25, in main
    cursesapp = curses.wrapper(setup)
  File "/usr/lib/python3.2/curses/wrapper.py", line 43, in wrapper
    return func(stdscr, *args, **kwds)
  File "quick_test.py", line 28, in setup
    CursesApp(stdscr)
  File "quick_test.py", line 9, in __init__
    self.mainLoop()
  File "quick_test.py", line 16, in mainLoop
    self.Screen.height=10 #This is the line that crashes, because it is
not an attribute of "stdscr".
AttributeError: '_curses.curses window' object has no attribute 'height'
****CODE/ERROR OUTPUT ABOVE****
> 
> -- 
> Steven

****CODE BELOW****
#!/usr/bin/python3
"""With this method we cause a TypeError, because the window object is
not callable."""
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() #So now we will initialize "self.Screen()" and we
will cause the TypeError.
        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
            self.Screen.height=10 #We never get here because we caused a
TypeError.

    class Screen:
        def __init__(self):
            self.height, self.width = self.getmaxyx()
            self.offsety, self.offsetx = -self.height/2, -self.width/2
            self.curx, self.cury = 1, 1

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

def setup(stdscr):
    CursesApp(stdscr)

if __name__ == '__main__':
    main()
****CODE ABOVE****
****CODE/ERROR OUTPUT BELOW****
$ python3 quick_test.py 
Traceback (most recent call last):
  File "quick_test.py", line 32, in <module>
    main()
  File "quick_test.py", line 26, in main
    cursesapp = curses.wrapper(setup)
  File "/usr/lib/python3.2/curses/wrapper.py", line 43, in wrapper
    return func(stdscr, *args, **kwds)
  File "quick_test.py", line 29, in setup
    CursesApp(stdscr)
  File "quick_test.py", line 8, in __init__
    self.Screen() #So now we will initialize "self.Screen()" and we will
cause the TypeError.
TypeError: '_curses.curses window' object is not callable
****CODE/ERROR OUTPUT ABOVE****

But this leaves me with four problems and I still would like a solution
if one is possible. 
Thank you for your time and help in understanding what I am doing wrong.
--
Jordan



More information about the Tutor mailing list