Mojo-Daten an Python übergeben
Übergeben Sie Werte über die Interop-Brücke.
Mojo-Daten an Python übergeben ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎯
Häufig gestellte Fragen
Ist die Lektion „Mojo-Daten an Python übergeben“ kostenlos?
Ja — der vollständige Text von „Mojo-Daten an Python übergeben“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Mojo-Daten an Python übergeben“?
Übergeben Sie Werte über die Interop-Brücke. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Mojo Academy zu starten?
Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Mojo-Daten an Python übergeben“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?
Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- NumPy aus Mojo verwenden
- Mojo-Daten an Python übergeben
- Python-Objekte zurücklesen
- Tücken der Interop-Performance