[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

a question in python curses modules

Marco

2/15/2008 3:55:00 AM

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!
--
LinuX Power
3 Answers

Marc 'BlackJack' Rintsch

2/15/2008 8:02:00 AM

0

On Fri, 15 Feb 2008 11:55:11 +0800, Marco wrote:

> 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

Sion Arrowsmith

2/15/2008 3:10:00 PM

0

Marc 'BlackJack' Rintsch <bj_666@gmx.net> wrote:
>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.

--
\S -- siona@chiark.greenend.org.uk -- http://www.chaos.org...
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

Marc 'BlackJack' Rintsch

2/15/2008 6:06:00 PM

0

On Fri, 15 Feb 2008 15:10:12 +0000, Sion Arrowsmith wrote:

> Marc 'BlackJack' Rintsch <bj_666@gmx.net> wrote:
>>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?

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