Java final vs Py __del__

Hi ! I very wonder, when I get exp. in java with GC. I'm Delphi programmer, so I get used to destructorin objects. In Java the final method is not same, but is like to destructor (I has been think...). And then I try with some examples, I see, that the Java GC is sometimes not call this method of objects, only exit from program. So: the java programs sometimes end before the GC is use the final methods on objects. This mean that in Java the critical operations MUST do correctly by the programmmers, or some data losing happened. If it is open a file, then must write the critical modifications, and must use the flush, and close to be sure to the datas are saved. In the Py the __del__ is same java's final, or it is to be called in every way by GC ? I build this method as safe method: if the programmer don't do any closing/freeing thing, I do that ? simple example: class a: def __init__(self,filename): self.__filename=filename self.__data=[] self.__file=None def open(self): self.__file=open(self.__filename,"w") def write(self,data): self.__data.append(data) def close(self): self.__file.writelines(self.__data) self.__file.close() self.__file=None def __del__(self): if self.__file<>None: self.close() # like destructor: we do the things are forgotten by programmer Thanx for infos: KK

On Thursday 27 November 2003 12:55 pm, Kepes Krisztian wrote:
Hi !
Hi Kepes. These questions are improper to pose here on Python-Dev, which is a mailing list about the development OF Python; for questions that are just related to Python programming, please send them to the general list, python-list@python.org, or help@python.org instead. I'm answering them this time, but please don't use this list again in the future unless it is for issues related to the development OF Python, thanks.
In Java the final method is not same, but is like to destructor (I has
You're confusing final (which is a Java keyword indicating a method that cannot be overridden in subclasses) with finalize -- there is no connection at all between these two concepts in Java. The Python _language_ gives just as few guarantees about calling finalizers (__del__ in Python) as Java (otherwise, it would not be possible to implement Python on top of a Java Virtual Machine, yet Jython, the Python implementation running on a JVM, works quite productively). Some specific implementation (such as a given release of "classic Python") may happen to do a bit more, but for reliability you will want to use try/finally in Python just as you would in Java. Alex

On Thursday 27 November 2003 12:55 pm, Kepes Krisztian wrote:
Hi !
Hi Kepes. These questions are improper to pose here on Python-Dev, which is a mailing list about the development OF Python; for questions that are just related to Python programming, please send them to the general list, python-list@python.org, or help@python.org instead. I'm answering them this time, but please don't use this list again in the future unless it is for issues related to the development OF Python, thanks.
In Java the final method is not same, but is like to destructor (I has
You're confusing final (which is a Java keyword indicating a method that cannot be overridden in subclasses) with finalize -- there is no connection at all between these two concepts in Java. The Python _language_ gives just as few guarantees about calling finalizers (__del__ in Python) as Java (otherwise, it would not be possible to implement Python on top of a Java Virtual Machine, yet Jython, the Python implementation running on a JVM, works quite productively). Some specific implementation (such as a given release of "classic Python") may happen to do a bit more, but for reliability you will want to use try/finally in Python just as you would in Java. Alex
participants (2)
-
Alex Martelli
-
Kepes Krisztian