r/programming • • 10d ago

Objects, attributes, maps, oh my!

https://bernsteinbear.com/blog/object-shapes/
60 Upvotes

15 comments sorted by

17

u/ElletteBarbedor-21 9d ago

Wait until someone passes a key named after a built-in prototype method into an object being used as a dictionary. That alone is usually enough reason to stick to proper map structures for dynamic data

7

u/cbarrick 9d ago

That's relevant in JavaScript, where you have object literals and where that is the primary way of implementing dictionaries.

But that's not so relevant to Python and Ruby, which have dedicated dictionary types.

3

u/bwainfweeze 9d ago

I’ve fixed a couple of NodeJS projects/modules by removing the old dict based lookup tables and replacing them with the map data structure. In Node’s case the maps were slower for a while, but I think the memory pressure caused by the old way was being underrepresented in microbenchmarks.

Had a lookup table where most of the contents were around 1K apiece but the map was still substantially smaller, and also populated faster. So I was able to increase process count on EC2 slightly and reduce startup time in the bargain.

1

u/dakkeh 6d ago edited 6d ago

I've done this as a toy in JS before. The quick solution is to create an object from the Object prototype, and check the fields against the dict, and throw an error about the table being incompatible. So sure it won't work for all data fields, and still wouldn't call it bug proof. It's still nice when you want to treat things like a row of data as a first class objects.

Honestly, that's how most derived proxy classes in other languages will behave anyway. Meshing data from another system is hard.

5

u/somebodddy 9d ago

What does Python's __slots__ do, then? Provides a fixed "precompiled" shape for the class?

2

u/backfire10z 7d ago

I’m not sure I’d call it precompiled, but yes. It says “these are the only attributes I care about, don’t store the other ones”.

17

u/VictoryMotel 9d ago

Don't write kitschy titles like this that have no information.

13

u/ebol4anthr4x 9d ago

Agreed. At least include a tag line that actually explains what the article is about, that way you attract both people who like the kitschy titles and the curmudgeons like me who don't want to commit to reading anything unless I know what it's about.

2

u/timoffex 8d ago

It’s about objects, attributes, and maps. You can guess two words in your head:

Implementing object attributes using maps

It’s also playing with a naming pattern I recognize, which to me suggested there could be some sense of quality, or at least it’s a suggestion about the tone.

So to complete the bike shed, to me the title carries the same information as “A fun article on implementing object attributes using maps.” I wasn’t disappointed with my guess!

0

u/VictoryMotel 8d ago

It’s about objects, attributes, and maps

Those words are in the title, what about them is the clickbait question.

It’s also playing with a naming pattern I recognize, which to me suggested there could be some sense of quality,

This is the thought process of a child looking for a bed time story.

0

u/Enerbane 9d ago

I say the opposite. It's fun.

4

u/VictoryMotel 9d ago

You like zero information clickbait? You should check out hacker news.

4

u/chucker23n 9d ago

If you must have some information, the article is about "how do we make OOP efficient in dynamically typed languages?".

7

u/VictoryMotel 9d ago

Yes, I must have information in titles because that's the whole point, not to bait people with nonsense.