[Tutor] how useful is __del__()

Carsten Bohnens germanator at gmx.de
Sat Apr 9 21:47:43 CEST 2005


hey,
I wrote a small class that does some ftp work for me:

#myftpclass.py
class myftp:
    def __init__(self, ...):
       ...
    def connect_and_login(self):
       ...
    def do_foo(self):
       ...
    def do_bar(self):
       ...
    #here comes the interesting part for me
    def goodbye(self):
       self.currenthost.quit()
       print 'Disconnected.'

my actual scripts then look like that:

#movesomedata.py

from myftpclass import myftp
ftp = myftp(--host, username, etc...--)
ftp.connect_and_login()
ftp.do_foo()
ftp.do_bar()
ftp.do_foo()
ftp.goodbye()


I am wondering if there is a way to get the disconnect without calling 
the funcion goodbye or doing ftp.currenthost.quit() by youself and if 
that could be done by defining a __del__() funcion in myftpclass. like, 
the end of the script is reached, or an exception is raised somewhere, 
disconnect.
or should I do something like this instead:

#movesomedata2.py
from myftpclass import myftp
try:
    ftp = myftp([host, username, etc...])
    ftp.connect_and_login()
    ftp.do_foo()
    ftp.do_bar()
    ftp.do_foo()
finally:
    ftp.goodbye()

thanks for all answers

Carsten

germanator at gmx dot de


More information about the Tutor mailing list