Reading Python Objects Back
Unpack results returned by Python.
Reading Python Objects Back is a free Mojo Academy lesson on CoddyKit — lesson 3 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.
Results Come Back
When a Python function returns, you get a value back in Mojo. Knowing how to unpack it lets you use the result in your code. 📦
Always a PythonObject
Every Python call returns a PythonObject, Mojo's wrapper around whatever Python produced, be it a number, string, or array.
var total = np.sum(a)Convert to Int
To use a result as a native Mojo number, convert it. Wrapping it with Int turns a Python integer into a Mojo Int.
var count = Int(py_len)Convert to Float
For decimal results, use Float64 to pull a Python float into a Mojo floating-point value you can compute with.
var mean = Float64(py_mean)Read Attributes
You can keep using dot access on a returned object to read its attributes, exactly as you would in Python itself.
var size = arr.sizeIndex Into Results
If the result is a sequence, index it with brackets. Each element is itself a PythonObject you can convert next.
var first = a[0]Call Methods Back
Returned objects keep their Python methods. Call them with dot syntax to transform results further before reading them.
var flat = arr.flatten()Convert to Mojo String
To turn a Python text result into native Mojo text, wrap it with String so you can store and join it normally.
var label = String(py_text)Convert Early
Once you need real Mojo computation, convert the result out of PythonObject. Staying wrapped keeps you slow inside the Python world.
Check for Truth
Python results can be tested in conditions too. Convert with Bool when you need a yes-or-no answer from a returned object.
if Bool(py_flag):
print("ready")Why It Matters
Reading results back lets you blend Python output with fast Mojo logic, getting answers out of libraries and into your kernels. ⚡
Quick Check
Recall how you turn a Python integer result into a usable Mojo number.
Recap
You learned every Python call returns a PythonObject, and how to convert it with Int, Float64, or String to use the result in Mojo. 🎯
Frequently asked questions
Is the “Reading Python Objects Back” lesson free?
Yes — the full text of “Reading Python Objects Back” 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 “Reading Python Objects Back”?
Unpack results returned by Python. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Reading Python Objects Back” 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