Re: Python language problem




Laszlo Nagy wrote:
You must undestand that 'a' and 'b' are names. You can only delete
names, not objects. Objects are freed by the garbage collector,
automatically. Probably you used to write programs in C or Pascal or
other languages with pointers. In Python, there are no pointers, just
references and you cannot free an object. You can only delete the
references to it. The good question is: why would you like to free an
object manually? The garbage collector will do it for you automatically,
when the object has no more references. (Well, cyclic references are
also garbage collected but not immediately.)

If you need to handle resources, you can still use the try-finally
statement. Like in:

f = file('example.txt','r')
try:
s = f.read()
finally:
f.close() # The resource is freed. But the object that was used to
access the resource, may not be freed here....

Regards,

Laszlo

Thanks for your so detailed explain, I think I know you, But my Engish
is not enough
to explain.

I create same object in Tree, I want to update Tree, when I need to
delete subtree.
If where no references, I can't do that. some thing like blow:
for i in list:
del i
From that code, I can't update list anymore.

.



Relevant Pages

  • Re: Python language problem
    ... Probably you used to write programs in C or Pascal or other languages with pointers. ... In Python, there are no pointers, just references and you cannot free an object. ... The garbage collector will do it for you automatically, when the object has no more references. ... But the object that was used to access the resource, ...
    (comp.lang.python)
  • Re: FastMM and garbage collection...
    ... It seems to me that the memory manager has all the information required ... Such references could be pretty much ... So how would your garbage collector determine which objects are no ... total control of all threads and have complete type information about ...
    (borland.public.delphi.language.basm)
  • Re: Setting objects to Nothing uses less memory!
    ... The garbage collector only does one pass and frees ... "bigObjects" and its internal instance of "lotsOfData" in the first ... > I think people often get confused about setting references to null. ... > The key is your InternalSetNull() method. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Dispose must be thread-safe ?
    ... > I admit that due to my limited knowledge of GC-ing in .NET I was not able to ... > other thread still references it and even calls Dispose method on it)?? ... method would include an implicit "root" (from the garbage collector's ... it doesn't stop the garbage collector from trying to ...
    (microsoft.public.dotnet.framework.clr)
  • Re: does python have useless destructors?
    ... "Called by the garbage collector on an object when garbage collection ... determines that there are no more references to the object." ... Let Java code ... > use Java idioms. ...
    (comp.lang.python)