destructor in python ?

All python classes do understand the "__del__" destructor.

i.e.:

def __del__ (self):
      self.close()

But: In my opinion every predefined python class (like your opened file
object) has a destructor which does reasonable things like closing itself
when the GC destroys the object.
So I think that an opened file whose last reference is gone would be closed
automagically.

Regards,
Mike

---------+---------------------------->
        > "Krisztian Kepes"|
        > <Kepes.Krisztian@|
        > peto.hu> |
        > >
        > 26.06.2003 13:11 |
        > Please respond to|
        > wxPython-users |
        > >
---------+---------------------------->

  >------------------------------------------------------------------------------------------------------------------------------|
  > >
  > To: <wxPython-users@lists.wxwindows.org> |
  > cc: |
  > Subject: [wxPython-users] destructor in python ? |
  >------------------------------------------------------------------------------------------------------------------------------|

Hi !

Have the any python object with an overrideable destructor ?

exampl.

class fileobj:
def __init(self,filename,access):
  self.__file=file(filename,access)

def writelog(self,log):
   self.__file.writelines(log)

def close(self)
  if self.__file==None: return(0)
  self.__file.close()
  self.__file=None

like this:

def __done__(self):
  self.close()

What the python do with onclosed connections/files ?

ftp=FTP()
ftp.connect(....)

f=file(filename,access)
f.read(....)

The reference is out, ok. But it must run an destructor to the object must
close self owned resources. If it is destroyed without destructor - all of
the resources are remaining in the background !

This is what I don't understand it dotnet too...

Thx:
KK

···

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

Not that this has anything to do with wxPython, but...

Michael_Perkonigg@uniquare.com wrote:

All python classes do understand the "__del__" destructor.

i.e.:

def __del__ (self):
     self.close()

But: In my opinion every predefined python class (like your opened file
object) has a destructor which does reasonable things like closing itself
when the GC destroys the object.
So I think that an opened file whose last reference is gone would be closed
automagically.

This is true, but keep in mind that there's no guarantee that a Python destructor will run when you think it does. The c-python implementation uses refcounting to collect garbage, and it just happens to do so as soon as the last reference goes away (presuming that it's not trapped in a reference cycle, which may take some time to free). However, Jython uses Java's garbage collection system, which means that your __del__() method may not get run until quite some time after your last reference goes away. If you have any open resources that really are important, or are shared with other processes, etc, then it really is best to explicitly free them. (You can write a cleanup() method, say, and maybe have __del__() call cleanup() if it hasn't already been called.)

By the way, in the original code example:

class fileobj:
def __init(self,filename,access):

This is probably just a typo, but the __init__() method needs to have double underscores both before *and* after the name, in order to be recognized as a "magic" method.

Jeff Shannon
Technician/Programmer
Credit International