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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:02:01+00:00 2026-05-15T11:02:01+00:00

Several times (even several in a row) I’ve been bitten by the defaultdict bug:

  • 0

Several times (even several in a row) I’ve been bitten by the defaultdict bug: forgetting that something is actually a defaultdict and treating it like a regular dictionary.

d = defaultdict(list)

...

try:
  v = d["key"]
except KeyError:
  print "Sorry, no dice!"

For those who have been bitten too, the problem is evident: when d has no key ‘key’, the v = d["key"] magically creates an empty list and assigns it to both d["key"] and v instead of raising an exception. Which can be quite a pain to track down if d comes from some module whose details one doesn’t remember very well.

I’m looking for a way to take the sting out of this bug. For me, the best solution would be to somehow disable a defaultdict’s magic before returning it to the client.

  • 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-15T11:02:01+00:00Added an answer on May 15, 2026 at 11:02 am

    You can prevent creation of default values by assigning d.default_factory = None. However, I don’t quite like the idea of object suddenly changing behavior. I’d prefer copying values to the new dict unless it imposes severe performance penalty.

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

Sidebar

Related Questions

I've gone through the options for this plugin several times and can't seem to
I have a line of code in JavaScript, using prototype, that is run several
I have several jQuery-ui range sliders on a page which add and remove rows
We have a WPF application, based on Unity with MMVVVM pattern. In application life
I am working on development of an application using CakePHP framework. We all have
i have a Log on in APS MVC 2.0 and if not logged on,
When I run F# compiler - fsc.exe - on our build server it takes
I am getting a lot of occurences of the message Debug session in progress.
I want to use the recursive_directory_iterator offered by boost::filesystem to delete a directory. But
I have a Subversion working copy with at least one missing file (the local

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.