Search code examples
classpython-3.xoopcursespython-curses

How can I extend a class in Python curses module


In Python curses, a new window object can be created with the curses.newwin() function. How can I extend the class of the objects that are instantiated from the newwin function?

I have tried dir(curses) but couldn't find any obvious class names there for me to extend.


Solution

  • curses.newwin returns a extension type.

    Unfortunately, you can't set attributes of extension types.

    As a workaround, you can redefine curses.newwin to return a new class instance that wraps the return value of the original curses.newwin(..):

    orig_newwin = curses.newwin
    def newwin(*args):
        win = orig_newwin(*args)
        return Wrapper(win)
    curses.newwin = newwin