Procedure for removing the list elements to the python.



  • In the code below, I create a list of Class A copies and then remove it.

    class A:
        def __init__(self, val):
            self.val = val
        def __del__(self):
            print self.val,
    

    object_list = [A(i) for i in range(5)]
    del object_list

    In order to understand the manner in which the elements are removed, I added a print to the method. del
    I expected to see the conclusion.
    0 1 2 3 4
    in http://docs.python.org/reference/simple_stmts.html#the-del-statement (if I understand correctly) it is written that the removal of the site list will take turns on the left to the right.

    The result is:
    4 3 2 1 0

    I understand that the documentation describes a case in which the expressions of the collateral are listed by several objects and then they are removed from the left to the right. But with the list transmitted, it's probably not the same. It would be highly appreciated if someone would have staggered me into the documentation place or any book that would explain the removal of the elements from the list when the list was removed.

    I have a python 2.6.

    upd. By the way, I found it. https://stackoverflow.com/questions/8205102/calling-del-on-a-list That's exactly the same question I've been looking for before.



  • http://docs.python.org/reference/datamodel.html#object.__del__ - it's a method called where applicable There are no references.

    The only guarantee we're facing. del list or del list[:] - that the number of references to the list elements decrease by 1 (in the first case also decreases by 1 number of references to the facility itself list)

    Order of call __del__ on components implementation-specific conduct del listand, in general, the circumference of elements in this method is not specific.

    Thus, depending on the implementation of the elements of the challenges __del__ may be as good as it is. 4 3 2 1 0And it's okay. 0 1 2 3 4


    If you add a little more specific, CPython Reversal of elements due to non-availability thrashing'a one casecreate and immediate release elements of a huge list.

    The following fragmentation of the function list_dealloc Total listobject.c implementation CPython

        /* Do it backwards, for Christian Tismer.
           There's a simple test case where somehow this reduces
           thrashing when a *very* large list is created and
           immediately deleted. */
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2