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

  • Home
  • SEARCH
  • 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 585277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:59:10+00:00 2026-05-13T14:59:10+00:00

What are the best practices and recommendations for using explicit del statement in python?

  • 0

What are the best practices and recommendations for using explicit del statement in python? I understand that it is used to remove attributes or dictionary/list elements and so on, but sometimes I see it used on local variables in code like this:

def action(x):
    result = None
    something = produce_something(x)
    if something:
        qux = foo(something)
        result = bar(qux, something)
        del qux
    del something
    return result

Are there any serious reasons for writing code like this?

Edit: consider qux and something to be something “simple” without a __del__ method.

  • 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-13T14:59:10+00:00Added an answer on May 13, 2026 at 2:59 pm

    I don’t remember when I last used del — the need for it is rare indeed, and typically limited to such tasks as cleaning up a module’s namespace after a needed import or the like.

    In particular, it’s not true, as another (now-deleted) answer claimed, that

    Using del is the only way to make sure
    a object’s __del__ method is called

    and it’s very important to understand this. To help, let’s make a class with a __del__ and check when it is called:

    >>> class visdel(object):
    ...   def __del__(self): print 'del', id(self)
    ... 
    >>> d = visdel()
    >>> a = list()
    >>> a.append(d)
    >>> del d
    >>>
    

    See? del doesn’t “make sure” that __del__ gets called: del removes one reference, and only the removal of the last reference causes __del__ to be called. So, also:

    >>> a.append(visdel())
    >>> a[:]=[1, 2, 3]
    del 550864
    del 551184
    

    when the last reference does go away (including in ways that don’t involve del, such as a slice assignment as in this case, or other rebindings of names and other slots), then __del__ gets called — whether del was ever involved in reducing the object’s references, or not, makes absolutely no difference whatsoever.

    So, unless you specifically need to clean up a namespace (typically a module’s namespace, but conceivably that of a class or instance) for some specific reason, don’t bother with del (it can be occasionally handy for removing an item from a container, but I’ve found that I’m often using the container’s pop method or item or slice assignment even for that!-).

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

Sidebar

Related Questions

What best practices have you used in unit testing embedded software that are peculiar
I’m looking for recommendations and best practices for applying signed assemblies in an organization
What are the current best practices with git branches that have been created to
What's the best practices way to test that a model is valid in rails?
I'm looking for recommendations on best practices when implementing equality in a domain model.
More than a specific answer I'm trying to get recommendations on best practices when
Any recommendations or best practices for mapping a subdomain to a Wordpress page: http://my-page.mydomain.com
Using VB.Net, I'm looking for best practices in dealing with the following idiom: For
Any best practices/recommendations on the installation folder for the Android SDK? Following http://developer.android.com/sdk/installing.html on
Best practices or tools for installing a SQL Server database I have a SQL

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.