Fundamentals 12 min read

Python dict, set, and frozenset: Common Methods, Subclassing, and Implementation Principles

This article explains Python's dict, set, and frozenset containers, lists their most frequently used methods, demonstrates how to subclass dict and create a container class, and describes the hash‑table implementation details that give these data structures their high performance.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Python dict, set, and frozenset: Common Methods, Subclassing, and Implementation Principles

This article introduces three core Python container types— dict , set , and frozenset —and provides a concise reference of their most common methods.

clear() - 清空字典中的所有条目。
copy() - 返回字典的一个浅拷贝。
fromkeys(sequence[, value]) - 创建一个新的字典,以序列中的元素作为键,可选的 value 作为默认值。
get(key[, default]) - 返回指定键的值,如果键不存在则返回 default(默认 None)。
items() - 返回键值对视图。
keys() - 返回键视图。
values() - 返回值视图。
pop(key[, default]) - 移除键并返回对应的值。
popitem() - 随机(LIFO)移除并返回一个键值对。
setdefault(key[, default]) - 若键不存在则插入并返回默认值。
update([other]) - 用另一个字典或可迭代对象更新当前字典。
__contains__(key) - 检查键是否存在。
__getitem__(key) - 获取键对应的值。
__setitem__(key, value) - 设置键的值。
__delitem__(key) - 删除键。

Example code demonstrates creating a dictionary, accessing values, adding and updating entries, iterating over keys, values, and items, and finally clearing the dictionary.

# 创建一个字典
d = {'name': 'Alice', 'age': 25, 'city': 'New York'}
# 获取值
print(d.get('name'))  # 输出: Alice
# 设置新值
d['job'] = 'Engineer'
# 更新字典
d.update({'age': 26, 'country': 'USA'})
# 获取所有键
print(d.keys())  # 输出: dict_keys([...])
# 获取所有值
print(d.values())  # 输出: dict_values([...])
# 获取键值对
print(d.items())  # 输出: dict_items([...])
# 删除键
del d['job']
# 检查是否包含键
print('job' in d)  # 输出: False
# 清空字典
d.clear()
print(d)  # 输出: {}

The article then shows how to subclass dict to add custom behavior, providing a CustomDict class with methods add_key , get_or_default , and display . It also discusses the trade‑offs of inheritance versus composition.

class CustomDict(dict):
    def __init__(self, *args, **kwargs):
        super(CustomDict, self).__init__(*args, **kwargs)
    def add_key(self, key, value):
        """Add a key‑value pair, raising KeyError if the key exists"""
        if key in self:
            raise KeyError(f"Key '{key}' already exists.")
        self[key] = value
    def get_or_default(self, key, default=None):
        """Return the value for key or default"""
        return self.get(key, default)
    def display(self):
        """Print all key‑value pairs"""
        for k, v in self.items():
            print(f"{k}: {v}")

custom_dict = CustomDict({'name': 'Alice', 'age': 25})
custom_dict.add_key('city', 'New York')
print(custom_dict.get_or_default('job', 'Unknown'))
custom_dict.display()

For composition, a CustomContainer class holds an internal dictionary and provides similar helper methods without directly subclassing dict .

class CustomContainer:
    def __init__(self, initial_data=None):
        self.data = {} if initial_data is None else dict(initial_data)
    def add_key(self, key, value):
        if key in self.data:
            raise KeyError(f"Key '{key}' already exists.")
        self.data[key] = value
    def get_or_default(self, key, default=None):
        return self.data.get(key, default)
    def display(self):
        for k, v in self.data.items():
            print(f"{k}: {v}")

The article then compares dict , set , and frozenset . dict stores key‑value pairs, set stores unique, unordered elements, and frozenset is an immutable version of set that can be used as a dictionary key.

Common methods for each type are listed, followed by short code snippets illustrating creation, addition, removal, and basic operations.

# dict example
 d = {'name': 'Alice', 'age': 25}
 print(d['name'])  # Alice
 d['city'] = 'New York'
 print(d)
# set example
 s = set([1, 2, 3, 4])
 s.add(5)
 print(s)  # {1, 2, 3, 4, 5}
 s.remove(3)
 print(s)  # {1, 2, 4, 5}
# frozenset example
 fs = frozenset([1, 2, 3, 4])
 print(fs)  # frozenset({1, 2, 3, 4})
 # fs.add(5) would raise AttributeError

Implementation details explain that both dict and set are built on hash tables in CPython. The hash function maps keys or elements to slots, collisions are resolved with open addressing (linear or quadratic probing), and the tables resize dynamically when the load factor grows. Keys and set elements must be immutable because their hash values must remain constant.

In CPython, dict is represented by the PyDictObject struct and set by PySetObject , both containing pointers to the underlying hash table and metadata such as size and capacity.

Finally, the article summarizes that the hash‑table implementation gives both containers O(1) average‑case performance for lookup, insertion, and deletion, while the choice among dict , set , and frozenset depends on whether you need key‑value mapping, an unordered mutable collection, or an immutable collection.

Pythondata structureshash tableset__dict__frozensetsubclassing
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.