Algorand Python SDK
A partial implementation of Python for the Algorand Virtual Machine
What is Algorand Python?
Algorand Python is a partial implementation of the Python programming language that runs on the Algorand Virtual Machine (AVM). It includes a statically typed framework for the development of Algorand Smart Contracts and Logic Signatures, with Pythonic interfaces to underlying AVM functionality.
Algorand Python is compiled for execution on the AVM by PuyaPy, an optimizing compiler that ensures the resulting AVM bytecode execution semantics match the given Python code. PuyaPy produces output that is directly compatible with AlgoKit typed clients, simplifying the process of deployment and calling. This allows developers to use standard Python tooling in their workflow.
Benefits of Using Algorand Python
Rapid Development
Python's concise syntax allows for quick prototyping and iteration of smart contract ideas.
Lower Barrier to Entry
Python's popularity means more developers can transition into blockchain development without learning a new language.
Ease of Use
Designed to work with standard Python tooling, making it easy for Python developers to start building smart contracts.
Efficiency
Compiled by PuyaPy optimizer that ensures AVM bytecode execution semantics match Python code.
Modularity
Supports modular and loosely coupled solution components, facilitating efficient parallel development by small teams.
Python Implementation for AVM
Algorand Python maintains the syntax and semantics of Python, supporting a subset of the language that will grow over time. However, due to the restricted nature of the AVM, it will never be a complete implementation. For example, async and await keywords are not supported as they don't make sense in the AVM context.
This partial implementation allows existing developer tools like IDE syntax highlighting, static type checkers, linters, and auto-formatters to work out of the box. This approach differs from other partial language implementations that add or alter language elements, which require custom tooling support and force developers to learn non-obvious differences from regular Python.
AVM Types and Python Equivalents
The basic types of the AVM are:
The AVM also supports "bounded" types, such as bigint (represented as BigUInt in Algorand Python), which is a variably sized (up to 512-bit) unsigned integer backed by bytes[].
It's important to note that these types don't directly map to standard Python primitives. For example, Python's int is signed and effectively unbounded, while a bytes object in Python is limited only by available memory. In contrast, an AVM bytes[] has a maximum length of 4096.
Python Primitives
Algorand Python has limitations on standard Python primitives due to the constraints of the Algorand Virtual Machine (AVM).
Supported Primitives
- • Bool: Algorand Python has full support for bool
- • Tuple: Python tuples are supported as arguments to subroutines, local variables, return types
- • typing.NamedTuple: Python named tuples are also supported using typing.NamedTuple
- • None: None is not supported as a value, but is supported as a type annotation to indicate a function returns no value
The int, str, and bytes built-in types are currently only supported as module-level constants or literals. They can be passed as arguments to various Algorand Python methods that support them or when interacting with certain AVM types.
Unsupported Primitives
- • Float: Not supported
- • Nested tuples: Not supported
Keep in mind, Python's int is unsigned and unbounded, while AVM's uint64 (represented as UInt64 in Algorand Python) is a 64-bit unsigned integer. Similarly, Python's bytes objects are limited only by available memory, whereas AVM's bytes[] (represented as Bytes in Algorand Python) have a maximum length of 4096 bytes.
Unsupported Python Features
Due to AVM Limitations
- • Exception handling: raise, try/except/finally not supported
- • Context managers: with statements not available
- • Async programming: async/await keywords not supported
- • Closures and lambdas: Function pointers not available
- • Global keyword: Module-level values must be constants
- • Inheritance: Limited to contract classes only
PuyaPy Compiler
The PuyaPy compiler is a multi-stage, optimizing compiler that takes Algorand Python and prepares it for execution on the Algorand Virtual Machine (AVM). It ensures that the resulting AVM bytecode execution semantics match the given Python code. The output produced by PuyaPy is directly compatible with AlgoKit typed clients, making deployment and calling of smart contracts easy.
The PuyaPy compiler is based on the Puya compiler architecture, which allows for multiple frontend languages to leverage the majority of the compiler logic. This makes adding new frontend languages for execution on Algorand relatively easy.
Testing and Debugging
The algorand-python-testing package allows for efficient unit testing of Algorand Python smart contracts in an offline environment. It emulates key AVM behaviors without requiring a network connection, offering fast and reliable testing capabilities with a familiar Pythonic interface.
# Example test structure
from algorand_python_testing import AlgodTestClient
def test_my_contract():
client = AlgodTestClient()
# Test your contract logic here
assert contract.method() == expected_resultBest Practices
Write Type-Safe Code
Always specify variable types, function parameters, and return values. Static typing is crucial in Algorand Python, differing significantly from standard Python's dynamic typing. This ensures type safety and helps prevent errors in smart contract development.
Leverage Existing Python Knowledge
Use familiar Python constructs and patterns where possible to maintain code readability and developer productivity.
Be Aware of AVM Limitations
When writing smart contracts, consider the restrictions imposed by the AVM and design your code accordingly to avoid unsupported features.
