Munch

4.0.0 · active · verified Thu Apr 09

Munch is a Python library providing a dot-accessible dictionary, similar to JavaScript objects. It's a widely used package in the Python ecosystem for handling configuration and data with intuitive attribute-style access. Actively maintained, it currently stands at version 4.0.0 with a steady release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic `Munch` object, initialize it from a dictionary (highlighting the shallow nature of the constructor versus the recursive `munchify` function), and use `DefaultMunch` for handling missing attributes gracefully.

from munch import Munch, munchify, DefaultMunch

# 1. Basic Munch object
data = Munch()
data.name = "Alice"
data.age = 30
print(f"Name: {data.name}, Age: {data.age}")

# Munch supports dictionary-style access too
data['city'] = 'New York'
print(f"City: {data.city}")

# 2. Initializing from a dictionary (shallow conversion)
regular_dict = {'user': {'id': 101, 'status': 'active'}}
m_shallow = Munch(regular_dict)
print(f"Type of nested 'user' in shallow Munch: {type(m_shallow.user)}") # Will be <class 'dict'>

# 3. Recursive conversion using munchify()
m_deep = munchify(regular_dict)
print(f"Type of nested 'user' in deep Munch: {type(m_deep.user)}") # Will be <class 'munch.Munch'>
print(f"User ID: {m_deep.user.id}")

# 4. DefaultMunch for default values
default_munch = DefaultMunch(0)
default_munch.count = 5
print(f"Existing count: {default_munch.count}") # Output: Existing count: 5
print(f"Non-existent value: {default_munch.non_existent}") # Output: Non-existent value: 0

view raw JSON →