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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:00:19+00:00 2026-06-01T03:00:19+00:00

Raymond Hettinger showed a really cool way to combine collection classes: from collections import

  • 0

Raymond Hettinger showed a really cool way to combine collection classes:

from collections import Counter, OrderedDict
class OrderedCounter(Counter, OrderedDict):
  pass
# if pickle support is desired, see original post

I want to do something similar for OrderedDict and defaultdict. But of course, defaultdict has a different __init__ signature, so it requires extra work. What’s the cleanest way to solve this problem? I use Python 3.3.

I found a good solution here: https://stackoverflow.com/a/4127426/336527, but I was thinking maybe deriving from defaultdict might make this even simpler?

  • 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-01T03:00:21+00:00Added an answer on June 1, 2026 at 3:00 am

    Inheriting from OrderedDict as in the answer you linked to is the simplest way. It is more work to implement an ordered store than it is to get default values from a factory function.

    All you need to implement for defaultdict is a bit of custom __init__ logic and the extremely simple __missing__.

    If you instead inherit from defaultdict, you have to delegate to or re-implement at least __setitem__, __delitem__ and __iter__ to reproduce the in-order operation. You still have to do setup work in __init__, though you might be able to inherit or simply leave out some of the other methods depending on your needs.

    Take a look at the original recipe or any of the others linked to from another Stack Overflow question for what that would entail.

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

Sidebar

Related Questions

I want a nice convenient attribute to do the following: from django.contrib.auth.models import User
I am trying to use an OrderedDict (Raymond Hettingers version for pre2.7 Python) where
Raymond Chen has confirmed that there is no publicly documented way to do this.
Raymond Hettinger has posted a snippet where he uses the sched module available in
[EDIT: thanks to MSalters answer and Raymond Chen's answer to InterlockedIncrement vs EnterCriticalSection/counter++/LeaveCriticalSection ,
Say I am inheriting from a class with several overloaded constructors. By any chance
At a post of Raymond Chen, he seems to be able to know the
Q. Is there a way to find out if an object has any "strong
I'm writing a lightweight class whose attributes are intended to be publicly accessible, and
I ran across Raymond Camden's blog, http://www.raymondcamden.com/index.cfm/2010/11/2/Building-a-simple-ColdFusion-TokenTemplate-System , and it is almost perfect for

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.