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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:58:32+00:00 2026-06-10T01:58:32+00:00

I implemented a class in python which currently inherits from dict, but really, I

  • 0

I implemented a class in python which currently inherits from dict, but really, I don’t want it to. The main reason for inheriting is so that I can use the **kwargs construct for copying the contents into an argument list.

I presume python does some sort of iteration over the dictionary, but I can’t find any documentation.

Is this possible, and, if so, how?

Code sample just to make things clearer:

   class MyThing():
       def __init__(self):
            self.dictionary = {}


   thing = MyThing()
   # code that causes thing.dictionary to be populated
   somefunc(**thing)

results in this:

TypeError: somefunc() argument after ** must be a mapping, not instance
  • 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-10T01:58:33+00:00Added an answer on June 10, 2026 at 1:58 am

    I went and read the python source, it seems to use the keys() and __getitem__ methods

    class A:
        def __init__(self, data):
            self.data = data
        def keys(self):
            return self.data.keys()
        def __getitem__(self, key):
            return self.data[key]
    
    def f(alpha):
        print alpha
    
    f(**A({'alpha': 2}))
    

    For the curious, If you use ** on a non-dict, CPython creates a new empty dict, and then calls dict.update() to copy your object’s keys into the dict.

    Its probably to best to implement this as a subclass of collections.Mapping

    class A(Mapping):
        def __init__(self, data):
            self.data = data
    
        def __iter__(self):
            return iter(self.data)
    
        def __len__(self):
            return len(self.data)
    
        def __getitem__(self, key):
            return self.data[key]
    

    Due to the inheritance from Mapping, pretty much all dict methods are supported. Given the error message, I think you can argue that this should qualify in all implementations of python.

    I’ve tried PyPy and it accepts the first version.

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

Sidebar

Related Questions

I want to be able use python descriptors in a class which has the
I have a Python class C which should have two pseudo- dict s a
I have written a class in python that implements __str__(self) but when I use
I want to test my class MyTypeDAO implemented with Hibernate 4.1 using JUnit 4.9.
I have a view in Eclipse (implemented by a class which extends org.eclipse.ui.part.ViewPart )
I want to implement a hash table in python. On the table a class
I have a class in python, which has an iterable as instance variable. I
I have a class method (implemented in a shared object in UNIX environment) which
I'm currently in the need for a Python container class with similar functionality like
I'm trying to implement a FUSE-driven filesystem in Python, which serves data from both

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.