Passing Mojo Data to Python
Hand values across the interop bridge.
Passing Mojo Data to Python is a free Mojo Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Crossing the Bridge
To use a Python library, your Mojo values must cross into Python first. That hand-off is the heart of interop. 🌉
Numbers Convert
Mojo numbers turn into Python numbers automatically when you pass them. A Mojo Int becomes a Python int the library can read.
var n = 42
var result = np.array([n])Build Python Lists
Many NumPy calls expect a Python list. You can write the list literal right inside the call, and Mojo passes it across the bridge.
var a = np.array([1, 2, 3, 4])The PythonObject Type
On the Python side, everything is a PythonObject. It is Mojo's wrapper for any value living in the Python world.
var obj: PythonObject = 3.14Wrap a Value
Annotate a value as PythonObject to convert it for Python. Mojo handles turning your number into the matching Python type.
var py_x: PythonObject = 10Strings Cross Too
Mojo strings convert into Python strings the same way, so you can pass text labels and names straight into Python functions.
var name: PythonObject = "mojo"Pass as Arguments
Once converted, just hand values to a Python call as arguments. The library receives normal Python objects and works as usual.
var scaled = np.multiply(a, 2)Mark It raises
Conversions and Python calls can fail, so wrap this work in a function marked raises to stay safe.
fn send() raises:
var py_x: PythonObject = 5Mind the Copy
Crossing the bridge often copies data into Python's memory. That copy has a cost, so pass big buffers thoughtfully.
Lists of Numbers
You can also pass a Mojo list of values into Python. Each element converts as it crosses, arriving as a Python list.
var data = [1.0, 2.0, 3.0]
var arr = np.array(data)Why It Matters
Smooth conversion means your Mojo numbers and text flow into any Python library, letting you reuse powerful tools without friction. ✨
Quick Check
Recall the type that represents a value once it lives on the Python side.
Recap
You saw Mojo numbers and strings convert into PythonObject values, ready to pass as arguments into any Python call across the bridge. 🎯
Frequently asked questions
Is the “Passing Mojo Data to Python” lesson free?
Yes — the full text of “Passing Mojo Data to Python” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “Passing Mojo Data to Python”?
Hand values across the interop bridge. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Passing Mojo Data to Python” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Using NumPy from Mojo
- Passing Mojo Data to Python
- Reading Python Objects Back
- Interop Performance Gotchas