a question in python curses modules

M

Marco

Hi, I wanna write a simple curses program, but somethings confuse
me, my code here:

#!/usr/bin/python

import os
import sys
import time
import curses

class CursesObject( object ):

def __init__(self):

self.STDSCR = curses.initscr()
curses.noecho()
curses.cbreak()
self.STDSCR.keypad(1)

def __del__(self):
self.STDSCR.keypad(0)
curses.nocbreak()
curses.echo()
curses.endwin()

c1 = CursesObject()
time.sleep(1)


I donot know what happen, but in __del__ function, curses become None??!!

Thank you VERY much!
 
M

Marc 'BlackJack' Rintsch

Hi, I wanna write a simple curses program, but somethings confuse
me, my code here:

#!/usr/bin/python

import os
import sys
import time
import curses

class CursesObject( object ):

def __init__(self):

self.STDSCR = curses.initscr()
curses.noecho()
curses.cbreak()
self.STDSCR.keypad(1)

def __del__(self):
self.STDSCR.keypad(0)
curses.nocbreak()
curses.echo()
curses.endwin()

c1 = CursesObject()
time.sleep(1)


I donot know what happen, but in __del__ function, curses become None??!!

When the interpreter shuts down it has to remove objects. Everything you
need in a `__del__()` method must be referenced by that object to be sure
that it is still there and not already garbage collected. *But* it's not
guaranteed that `__del__()` is called at all! So if you think this clean
up is necessary to leave a usable console then don't put it into a
`__del__()` method!

Ciao,
Marc 'BlackJack' Rintsch
 
S

Sion Arrowsmith

Marc 'BlackJack' Rintsch said:
When the interpreter shuts down it has to remove objects. Everything you
need in a `__del__()` method must be referenced by that object to be sure
that it is still there and not already garbage collected. *But* it's not
guaranteed that `__del__()` is called at all!

This may be true, but it's not really the point here, since clearly
__del__() *is* being called, otherwise how would the OP know that
curses was None in it?

What's relevant is the consequences of the first two sentences. As
the interpreter shuts down, it removes objects *and you don't know
what order it's going to do that in*. So what is happening here is
that first the curses module is being removed (and the name "curses"
bound to None instead), and then the CursesObject instance is
removed, which causes its __del__ to be called with curses == None.
 
M

Marc 'BlackJack' Rintsch

This may be true, but it's not really the point here, since clearly
__del__() *is* being called, otherwise how would the OP know that
curses was None in it?

It's not the point the OP asked for directly but just the answer in the
first two sentences might have left the impression it's okay to use
`__del__()` for this kind of clean up if you make sure that `curses` is
bound to the object. Then it may appear to work for the OP but it's not
guaranteed to work under all circumstances.

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top