Memento API Documentation

class Memento(state)[source]

Bases: object

Stores the state of the Originator.

Parameters:

state (str)

__init__(state)[source]

Initializes the Memento with the state.

Parameters:

state (str) – The state to save.

get_state()[source]

Retrieves the stored state.

Return type:

str

Returns:

The saved state.

class Originator[source]

Bases: object

Creates and restores Mementos to manage its state.

__init__()[source]

Initializes the Originator with no state.

set_state(state)[source]

Sets the state of the Originator.

Parameters:

state (str) – The new state.

get_state()[source]

Gets the current state of the Originator.

Return type:

str

Returns:

The current state.

create_memento()[source]

Creates a Memento with the current state.

Return type:

Memento

Returns:

A Memento containing the current state.

restore_memento(memento)[source]

Restores the state from a Memento.

Parameters:

memento (Memento) – The Memento to restore from.

class Caretaker[source]

Bases: object

Manages Mementos to track the history of the Originator’s states.

__init__()[source]

Initializes the Caretaker with an empty list of Mementos.

add_memento(memento)[source]

Adds a Memento to the history.

Parameters:

memento (Memento) – The Memento to add.

get_memento(index)[source]

Retrieves a Memento by index.

Parameters:

index (int) – The index of the desired Memento.

Return type:

Memento

Returns:

The Memento at the specified index.

Raises:

IndexError – If the index is out of bounds.