Overview
Monty supports iterative execution, allowing you to pause and resume Python code execution. This is useful for:- Handling external function calls with custom logic
- Implementing async external functions
- Debugging and step-through execution
- Dynamic name resolution
Monty.start() or resume() on a snapshot, execution returns one of three types:
MontySnapshot- Paused at an external function callMontyNameLookup- Paused at an undefined name lookupMontyComplete- Execution finished successfully
MontySnapshot
Represents paused execution waiting for an external function call to be resolved.Properties
string
The name of the script being executed
string
The name of the external function being called
any[]
Positional arguments passed to the external function
Record<string, any>
Keyword arguments passed to the external function as an object
Methods
resume()
ResumeOptions
required
Must contain either
returnValue or exception, but not bothMontySnapshot | MontyNameLookup | MontyComplete
MontySnapshot- Paused at another external function callMontyNameLookup- Paused at a name lookupMontyComplete- Execution completed
MontyRuntimeError- If the code raises an exception
dump()
Buffer
Binary representation of the snapshot
load() (static)
Buffer
required
Binary data from
dump()SnapshotLoadOptions
Optional configuration for loading
MontySnapshot
Restored snapshot ready to resume
Example: Basic Iteration
Example: Error Injection
Example: Serialization
MontyNameLookup
Represents paused execution waiting for a name (variable) to be resolved.Properties
string
The name of the script being executed
string
The name of the variable being looked up
Methods
resume()
NameLookupResumeOptions
MontySnapshot | MontyNameLookup | MontyComplete
MontySnapshot- Paused at an external function callMontyNameLookup- Paused at another name lookupMontyComplete- Execution completed
dump() / load()
Same asMontySnapshot - serialize and deserialize name lookup state.
Example: Dynamic Name Resolution
MontyComplete
Represents successfully completed execution with a final output value.Properties
any
The final result value from the executed code (the result of the last expression)
Example
Complete Example: Async External Functions
Combining snapshots with async functions:See Also
- Monty Class - Main interpreter class
- Errors - Error handling
