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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:06:50+00:00 2026-05-12T14:06:50+00:00

What are advantages of using UserDict class? I mean, what I really get if

  • 0

What are advantages of using UserDict class?

I mean, what I really get if instead of

class MyClass(object):
    def __init__(self):
        self.a = 0
        self.b = 0
...
m = MyClass()
m.a = 5
m.b = 7

I will write the following:

class MyClass(UserDict):
    def __init__(self):
        UserDict.__init__(self)
        self["a"] = 0
        self["b"] = 0
...
m = MyClass()
m["a"] = 5
m["b"] = 7

Edit: If I understand right I can add new fields to an object in a runtime in both cases?

m.c = "Cool"

and

m["c"] = "Cool"
  • 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-12T14:06:51+00:00Added an answer on May 12, 2026 at 2:06 pm

    UserDict.UserDict has no substantial added value since Python 2.2, since, as @gs mention, you can now subclass dict directly — it exists only for backwards compatibility with Python 2.1 and earlier, when builtin types could not be subclasses. Still, it was kept in Python 3 (now in its proper place in the collections module) since, as the docs now mention,

    The need for this class has been
    partially supplanted by the ability to
    subclass directly from dict; however,
    this class can be easier to work with
    because the underlying dictionary is
    accessible as an attribute.

    UserDict.DictMixin, in Python 2, is quite handy — as the docs say,

    The module defines a mixin, DictMixin,
    defining all dictionary methods for
    classes that already have a minimum
    mapping interface. This greatly
    simplifies writing classes that need
    to be substitutable for dictionaries
    (such as the shelve module).

    You subclass it, define some fundamental methods (at least __getitem__, which is sufficient for a read-only mapping without the ability to get keys or iterate; also keys if you need those abilities; possibly __setitem__, and you have a R/W mapping without the ability of removing items; add __delitem__ for full capability, and possibly override other methods for reasons of performance), and get a full-fledged implementation of dict‘s rich API (update, get, and so on). A great example of the Template Method design pattern.

    In Python 3, DictMixin is gone; you can get almost the same functionality by relying on collections.MutableMapping instead (or just collections.Mapping for R/O mappings). It’s a bit more elegant, though not QUITE as handy (see this issue, which was closed with “won’t fix”; the short discussion is worth reading).

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

Sidebar

Related Questions

What are the advantages of using the <fieldset> tag? I don't really get what
What would be some advantages of using etags/stale?/fresh_when? instead of page caching (on a
Could someone tell me the advantages to using the ConfigurationManager class which load's a
Regarding separations of concerns only, are there advantages of using ASP.NET MVC instead of
What are the advantages of using an ESB instead of directly accessing a database
Are there any advantages of using wildcard-type generics in the Bar class over completely
I can name three advantages to using double (or float ) instead of decimal
Are there any advantages to using <div id="here" ... instead of <div name="here" ...
Are there advantages of using Emacs instead of Eclipse for Android development? If so,
Are there any advantages to using the first, generic-based method instead of the second

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.