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 7880351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:07:31+00:00 2026-06-03T04:07:31+00:00

If a dictionary contains mutable objects or objects of custom classes (say a queryset,

  • 0

If a dictionary contains mutable objects or objects of custom classes (say a queryset, or a even a DateTime), then will calling clear() on the dictionary delete these objects from memory?
Does it behave differently than looping through the dict and deleting them?

eg.
consider

class MyClass(object):
    '''Test Class.'''

my_obj_1 = MyClass()
my_obj_2 = MyClass()

my_dict = { 'foo' : my_obj_1, 'bar' : my_obj_2 }

then is

my_dict.clear()

same as

for key in my_dict.keys():
    del my_dict[key]

?

  • 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-06-03T04:07:32+00:00Added an answer on June 3, 2026 at 4:07 am

    Python documentation on dicts states that del d[key] removes d[key] from the dictionary while d.clear() removes every key, so basically their behavior is the same.

    On the memory issue, in Python when you “delete” you are basically removing a reference to an object. When an object is not referenced by any variable nor other object or becomes unreachable, it becomes garbage and can be removed from memory. Python has a garbage collector that from time to time it does this job of checking which objects are garbage and releases the memory allocated for them.
    If the object you are deleting from the dictionary is referenced by other variable then it is still reachable, thus it is not garbage so it won’t be deleted. I leave you here some links if you are interested in reading about garbage collection in general and python’s garbage collection in particular.

    • http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)
    • http://www.digi.com/wiki/developer/index.php/Python_Garbage_Collection
    • http://www.doughellmann.com/PyMOTW/gc/
    • http://docs.python.org/library/gc.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DICOM dictionary that contains a set of objects all deriving from
I have a custom plist file which contains dictionaries. Each dictionary contains information about
I'm using VS 2005 fx2.0. If I know my Dictionary contains only 1 item
I have a list which contains a dictionary of ExpandoObjects. Im binding this to
I have a list which contains dictionaries. Each dictionary has an attribute description which
I have a class (singleton) and it contains a static Dictionary private static Dictionary<string,
I am trying to create a dictionary from 2 lists where one list contains
I have a simple text dictionary file, which contains words, separated by ';'.My problem
Please explain why dictionary's 'getAt' method fails List<BString> infoKeys = new List<BString>(infoDict.Keys); if (infoKeys.Contains(TorrentFileKeyWords.FILES_KEY)
Suppose I have a Collection of some kind that itself contains Collections; e.g., Dictionary(Of

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.