template<typename Key, typename T>
py::dict class

Python-like dictionary implementation.

Template parameters
Key The key type
T The value type

A dictionary class that extends std::unordered_map with Python-like convenience methods and functionality.

Constructors, destructors, conversion operators

dict()
Default constructor.
dict(std::initializer_list<value_type> init)
Construct a dict from an initializer list.
dict(dict<Key, T>&&) defaulted noexcept
Move Constructor (default)
dict(const dict<Key, T>&) defaulted
Construct a new dict object.

Public functions

auto contains(const Key& key) const -> bool -> auto
Check if the dictionary contains a specific key.
auto get(const Key& key, const T& default_value) const -> T -> auto
Get a value with a default fallback.
auto begin() > -> auto
Get iterator to the beginning of keys.
auto end() > -> auto
Get iterator to the end of keys.
auto begin() > -> auto
Get const iterator to the beginning of keys.
auto end() > -> auto
Get const iterator to the end of keys.
auto items() -> Base & -> auto
Get the underlying map for iteration.
auto items() const -> const Base & -> auto
Get the underlying map for iteration (const version)
auto copy() const -> Self -> auto
Create a copy of the dictionary.
auto operator[](const Key& k) const -> const T & -> auto
Access value by key (const version)
auto at(const Key& k) const -> const T & -> auto
Access value by key with bounds checking (const version)
auto operator[](const Key& k) -> T & -> auto
Access or insert value by key.
auto operator=(const Self&) -> Self & -> auto deleted
Copy assignment operator (deleted)
auto operator=(Self&&) noexcept -> dict & -> auto defaulted
Move assignment operator.

Function documentation

template<typename Key, typename T>
py::dict<Key, T>::dict()

Default constructor.

Creates an empty dictionary.

template<typename Key, typename T>
py::dict<Key, T>::dict(std::initializer_list<value_type> init)

Construct a dict from an initializer list.

Parameters
init in Initializer list of key-value pairs

Creates a dictionary from a list of key-value pairs.

template<typename Key, typename T>
py::dict<Key, T>::dict(const dict<Key, T>&) defaulted

Construct a new dict object.

Copy through explicitly the public copy() function!!!

template<typename Key, typename T>
auto py::dict<Key, T>::contains(const Key& key) const -> bool

Check if the dictionary contains a specific key.

Parameters
key in The key to look up
Returns true if the key is contained in the dictionary, false otherwise

template<typename Key, typename T>
auto py::dict<Key, T>::get(const Key& key, const T& default_value) const -> T

Get a value with a default fallback.

Parameters
key in The key to look up
default_value in The default value to return if key is not found
Returns T The value associated with the key, or the default value

Returns the value associated with the key, or the default value if the key is not found in the dictionary.

template<typename Key, typename T>
auto py::dict<Key, T>::begin() >

Get iterator to the beginning of keys.

Returns auto Iterator to the first key

Returns an iterator that yields keys from the dictionary.

template<typename Key, typename T>
auto py::dict<Key, T>::end() >

Get iterator to the end of keys.

Returns auto Iterator past the last key

Returns an iterator past the last key in the dictionary.

template<typename Key, typename T>
auto py::dict<Key, T>::begin() >

Get const iterator to the beginning of keys.

Returns auto Const iterator to the first key

Returns a const iterator that yields keys from the dictionary.

template<typename Key, typename T>
auto py::dict<Key, T>::end() >

Get const iterator to the end of keys.

Returns auto Const iterator past the last key

Returns a const iterator past the last key in the dictionary.

template<typename Key, typename T>
auto py::dict<Key, T>::items() -> Base &

Get the underlying map for iteration.

Returns std::unordered_map<Key, T>& Reference to the underlying map

template<typename Key, typename T>
auto py::dict<Key, T>::items() const -> const Base &

Get the underlying map for iteration (const version)

Returns const std::unordered_map<Key, T>& Const reference to the underlying map

template<typename Key, typename T>
auto py::dict<Key, T>::copy() const -> Self

Create a copy of the dictionary.

Returns Self A copy of this dictionary

template<typename Key, typename T>
auto py::dict<Key, T>::operator[](const Key& k) const -> const T &

Access value by key (const version)

Parameters
in The key to look up
Returns const T& Reference to the value

Returns a const reference to the value associated with the key. Throws std::out_of_range if the key is not found.

template<typename Key, typename T>
auto py::dict<Key, T>::at(const Key& k) const -> const T &

Access value by key with bounds checking (const version)

Parameters
in The key to look up
Returns const T& Reference to the value

Returns a const reference to the value associated with the key. Throws std::out_of_range if the key is not found.

template<typename Key, typename T>
auto py::dict<Key, T>::operator[](const Key& k) -> T &

Access or insert value by key.

Parameters
in The key to look up
Returns T& Reference to the value

Returns a reference to the value associated with the key. If the key does not exist, inserts a default-constructed value.

template<typename Key, typename T>
auto py::dict<Key, T>::operator=(const Self&) -> Self & deleted

Copy assignment operator (deleted)

Use copy() method instead for explicit copying.

template<typename Key, typename T>
auto py::dict<Key, T>::operator=(Self&&) noexcept -> dict & defaulted

Move assignment operator.

Returns Self& Reference to this dictionary