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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:14:45+00:00 2026-05-29T05:14:45+00:00

After some time programming in Javascript I have grown a little fond of the

  • 0

After some time programming in Javascript I have grown a little fond of the duality there between objects and associative arrays (dictionaries):

//Javascript
var stuff = { a: 17, b: 42 };

stuff.a;    //direct access    (good sugar for basic use)
stuff['a']; //key based access (good for flexibility and for foreach loops)

In python there are basically two ways to do this kind of thing (as far as I know)

Dictionaries:

stuff = { 'a': 17, 'b':42 };

# no direct access :(
stuff['a'] #key based access

or Objects:

#use a dummy class since instantiating object does not let me set things
class O(object):
    pass

stuff = O()
stuff.a = 17
stuff.a = 42

stuff.a #direct access :)
getattr(stuff, 'a') #key based access

edit: Some responses also mention namedtuples as a buitin way to create lighweight classes for immutable objects.


So my questions are:

  1. Are there any established best-practices regarding whether I should use dicts or objects for storing simple, method-less key-value pairs?

  2. I can imagine there are many ways to create little helper classes to make the object approach less ugly (for example, something that receives a dict on the constructor and then overrides __getattribute__). Is it a good idea or am I over-thinking it?

    • If this is a good thing to do, what would be the nicest approach? Also, would there be any good Python projects using said approach that I might take inspiration from?
  • 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-29T05:14:46+00:00Added an answer on May 29, 2026 at 5:14 am

    Not sure about “established best practices”, but what I do is:

    1. If the value types are homogenous – i.e. all values in the mappings are numbers, use a dict.
    2. If the values are heterogenous, and if the mapping always has a given more or less constant set of keys, use an object. (Preferrably use an actual class, since this smells a lot like a data type.)
    3. If the values are heterogenous, but the keys in the mapping change, flip a coin. I’m not sure how often this pattern comes up with Python, dictionaries like this notably appear in Javascript to “fake” functions with keyword arguments. Python already has those, and **kwargs is a dict, so I’d go with dicts.

    Or to put it another way, represent instances of data types with objects. Represent ad-hoc or temporary mappings with dicts. Swallow having to use the ['key'] syntax – making Python feel like Javascript just feels forced to me.

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

Sidebar

Related Questions

After some time I wanted to update my git repo, and then something went
Possible Duplicate: how to hide a div after some time period? i need to
I remember first learning about vectors in the STL and after some time, I
I had recently a problem with oom-killer starting to kill processes after some time.
After spending some time wireframing my ideas, I want to start building my rails
After a positive experience with Venkman for XUL development some time ago, I am
So i'm trying out MVC after only playing with it some time ago. Need
I've been programming procedural code for quite some time, and recently I've been learning
I am completely new to javascript programming and I have a question that I
After some time this actor fills out stack. Possible solutions ? object Puller extends

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.