Skip to content

Quick Start

Chris Griffith edited this page Nov 2, 2024 · 2 revisions

Basic Usage

from box import Box

my_box = Box(key="value")

print(my_box.key) 
# value

That is the equivalent of the following dictionary:

my_dict = dict(key="value")

print(my_dict["key"])
# value

Keep in mind, at any time you can also access or modify the Box in the same manor as a dictionary.

print(my_box["key"])
# value

Sub Boxes

my_box = Box({"team": {"red": {"leader": "Sarge", "members": []}}})
print(my_box.team.red.leader)
# Sarge

Any time a dict or list is added to the Box, it is also updated to a Box or BoxList

my_box.team.blue = {"leader": "Church", "members": []} 

print(repr(my_box.team.blue))
# <Box: {'leader': 'Church', 'members': []}>

Access Boxes Inside Lists

my_box.team.red.members = [
    {"name": "Grif", "rank": "Minor Junior Private Negative First Class"},
    {"name": "Dick Simmons", "rank": "Captain"}
]

print(my_box.team.red.members[0].name)
# Grif

How it works

Make sure to check out the limitations and how Box handles conversions to understand what edge cases may affect you!