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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:04:56+00:00 2026-06-06T07:04:56+00:00

I want to do something like this: class Dictable: def dict(self): raise NotImplementedError class

  • 0

I want to do something like this:

class Dictable:
    def dict(self):
        raise NotImplementedError

class Foo(Dictable):
    def dict(self):
        return {'bar1': self.bar1, 'bar2': self.bar2}

Is there a more pythonic way to do this? For example, is it possible to overload the built-in conversion dict(...)? Note that I don’t necessarily want to return all the member variables of Foo, I’d rather have each class decide what to return.

Thanks.

  • 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-06T07:04:58+00:00Added an answer on June 6, 2026 at 7:04 am

    The Pythonic way depends on what you want to do. If your objects shouldn’t be regarded as mappings in their own right, then a dict method is perfectly fine, but you shouldn’t “overload” dict to handle dictables. Whether or not you need the base class depends on whether you want to do isinstance(x, Dictable); note that hasattr(x, "dict") would serve pretty much the same purpose.

    If the classes are conceptually mappings of keys to values, then implementing the Mapping protocol seems appropriate. I.e., you’d implement

    • __getitem__
    • __iter__
    • __len__

    and inherit from collections.Mapping to get the other methods. Then you get dict(Foo()) for free. Example:

    class Foo(Mapping):
        def __getitem__(self, key):
            if key not in ("bar1", "bar2"):
                raise KeyError("{} not found".format(repr(key))
            return getattr(self, key)
    
        def __iter__(self):
            yield "bar1"
            yield "bar2"
    
        def __len__(self):
            return 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do something like this: class Foo(object): def __init__(self, name): self._name =
I want to do something like this: class Cls { function fun($php) { return
Essentially I want to do something like this: class foo: x = 4 @property
I want something like this: class Foo<T>{...} class Boo<T>{ Queue<T> stuff = new Queue<T>();
I want to convert something like this: class NestedItem attr_accessor :key, :children def initialize(key,
I want to do something like this: public class ScadenzaService { ... public List<Scadenza>
I want to do something like this: [Display(Name = Plain text. <span class=\red strong\>Red
I want something like this: abcdab.search(/a/g) //return [0,4] Is it possible?
I want to do something like this with a GridView: <asp:CommandField ShowSelectButton=True Visible='<%# return
i want something like this : public class Order { public Guid OrderID {

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.