Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6829123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:27:46+00:00 2026-05-26T22:27:46+00:00

class ToBeDeleted: def __init__(self, value): self.value = val # Whatever… def __del__(self): print self.value

  • 0
class ToBeDeleted:
    def __init__(self, value):
        self.value = val

    # Whatever...

    def __del__(self):
        print self.value

l = [ToBeDeleted(i) for i in range(3)]
del l

This prints 2, 1, 0.


  • Now, is the order of the deleted elements defined somewhere in specification or is it implementation-specific? (or maybe I don’t understand the underlying mechanics)

  • Could the output, for example, be 0, 1, 2? I realize the 2, 1, 0 order is probably done to avoid the memory reallocations for the elements while deleting them, but still the question remains.

  • And the last one – what’s the difference between del l and del l[:] statements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:27:47+00:00Added an answer on May 26, 2026 at 10:27 pm

    Running del l will remove any reference to the list, so the symbol l will be gone. In contrast, running del l[:] removes the contents of the list, leaving l as an empty list.

    The __del__ method is what runs when the last reference to an instance is being destroyed.

    The order of deletion isn’t specified and is implementation specific. When you run del l, the only thing that is guaranteed is that the reference count for the list l and each of its elements will decrease by one.

    With pypy, nothing else will happen until the garbage collector is run. The order of the object removal depends on the order that GC visits the objects.

    In cpython, the OP was correct in observing that reference decrementing occurs right-to-left. When invoking del l[:] here is the code used to decrement the refcounts: http://hg.python.org/cpython/file/2.7/Objects/listobject.c#l700 . When del l is invoked, similar code is used to decrement the refcounts: http://hg.python.org/cpython/file/2.7/Objects/listobject.c#l596

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Ball: a = [] def __init__(self): pass def add(self,thing): self.a.append(thing) def size(self): print
class A: def x ( self ): print( self.__class__ ) class B ( A
class Foo(object): def __init__(self,x): self.x = x self.is_bar = False def __repr__(self): return str(self.x)
class Test1: def __init__(self): self.x = 1 class Test2(Test1): # how can I get
class Article(models.Model): def user_may_see_full_version(self, user): # do something very sophisticated with the user return
class MainPage(webapp.RequestHandler): def get(self): user = users.get_current_user() tasks_query = Task.all() tasks = tasks_query.fetch(1000) if
class ParentClass { public function list() { foreach ($this as $property => $value) {
class CreateMatches < ActiveRecord::Migration def self.up create_table :matches do |t| t.integer :result_home t.integer :result_away
class Program { static void Main(string[] args) { string value = "12345"; Type enumType
class Person { string name; public Person(string name) { this.name = name; } public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.