Python dictionary with fixed __dict__ contents and variable arguments

Question:

I’m trying to read in a bunch of data from text files to a dictionary, and I’ve made a couple of small typos which end up creating new key/value pairs and breaking the program (which can be annoying and a pain to debug).

This got me wondering if there is a way to create a dictionary to which no keys can be added or removed after the initial declaration. This strikes me as something that must exist but I can’t find an example of it; does anyone know of something that would fit the bill?

def change_key(dictionary, key, new_value):
    a = dictionary.__len__()
    dictionary[key] = new_value
    if dictionary.__len__() != a:
        raise ValueError("new key added")

What I’m looking for is a more elegant implementation of the code above that is:

  1. not a massive mess of code

  2. capable of dealing with things like appending to a nested list or adding to a nested dict

  3. reasonably efficient

Asked By: Adam Ladd

||

Answers:

I believe what you’re looking for is a dataclass with __slots__ method (In Python 3.10, this can be achieved with @dataclass(slots=True)):

from dataclasses import dataclass, asdict


@dataclass
class HashMap:
    __slots__ = ("key_1", "key_2", "key_3")
    key_1: str
    key_2: str
    key_3: str


d = HashMap(key_1="something", key_2="something_else", key_3="another_thing")
d.key_1 = "updated_value"  # This will work fine
d.key_that_was_not_predefined = "some value"  # This will raise AttributeError
d = asdict(d)  # This converts a dataclass instance to a normal dictionary
Answered By: Vladimir Vilimaitis
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.