how to count lines in a file ?

Steve Holden sholden at holdenweb.com
Fri Jul 26 23:44:45 EDT 2002


"Jonathan Hogg" <jonathan at onegoodidea.com> wrote ...
> "Steve Holden" <sholden at holdenweb.com> wrote:
>
> > So what you appear to dislike is that there is now a *chance* that
__del__()
> > will be called on cyclic garbage, when there wasn't before.
>
> Not quite. Any cycle containing an object with a __del__ method will be
> skipped and left to languish in memory-limbo forever.
>
You mean the garbage collector doesn't *collect* data that appears in
cycles? That's not my understanding (which doesn't nevessarily mean it's
dissonant with reality).

> If the objects exist in a cycle then "logically" they must exist for ever.

This simply isn't true. If you create a list or a dictionary that refers to
itself (perfectly possible), that object is collectable as soon as *nothing
else* refers to it. For example:

>>> a = [1,2,3,4]
>>> a.append(a)
>>> a
[1, 2, 3, 4, [...]]
>>>

Notice how the interpreter cleverly avoids an infinite recursion in the
repr() of that list. The fact that it contains references to itelf need not
stop its collection, and that is precisely why [I understand] the new GC
scheme was introduced to supplement reference counting. If I now execute the
following statement:

>>> a = "No more references"

there is absolutely no reason why the list's elements, and the space
occupied by the list, need not be returned to the free poll (assuming other
references have not been created).

> Luckily, for most simple containers, throwing them away is semantically no
> different from keeping them around - a dictionary or a list on it's own
> doesn't do anything. However, if something has a __del__ method then
> throwing it away requires executing some code which may have unknown
> side-effects - so you can't do it.
>
So, what you *seem* to be saying is that it would be p[erfectly possible to
collect objects thatdon't have __del__() methods, but once they acquire such
they are no longer collectible even when only cyclic references exist? That
seems an odd asserion.

> At the very simplest level, the object's dictionary/slots may be
meaningless
> at the time that you would execute __del__ (because you're in the middle
of
> breaking all the cyclic references), so executing __del__ might cause
> horrible things to happen.
>
Well, certainly you *always* have to be careful that references haven't been
invalidated before use.

> It's a sort of philosophical thing. If you can be sure that having closed
> the door, no-one will open it again then the room on the other side is
> allowed to simply disappear ;-)
>
> __del__ only has sensible semantics in the presence of reference counting.
> Either have reference counting or throw away __del__. When it comes down
to
> it you gotta pick one or the other.
>
I'm afraid you're going to have to explain that one. I don't think there is
any great philosophical difference bvetween ourpositions, but I'm not yuet
completely sure I understand what you are saying. As the Reverend Sydney
Smith observed upon seeing two women shouting at each other from opposite
windows: "I fear they will never agree, for they are arguing from different
premises".

wanting-to-agree-ly y'rs  - steve
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list