0Pricing
Mojo Academy · Lezione

Passare dati Mojo a Python

Trasferisca valori attraverso il bridge di interoperabilità.

Passare dati Mojo a Python è una lezione Mojo Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Mojo Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Mojo Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.14

Wrap 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 = 10

Strings 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 = 5

Mind 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. 🎯

Domande Frequenti

La lezione «Passare dati Mojo a Python» è gratuita?

Sì — il testo completo di «Passare dati Mojo a Python» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Mojo Academy, passa a CoddyKit PRO. Il corso Mojo Academy include 4 lezioni in totale.

Cosa imparerò in «Passare dati Mojo a Python»?

Trasferisca valori attraverso il bridge di interoperabilità. Eserciti Mojo Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Mojo Academy?

Non è richiesta alcuna esperienza precedente. Mojo Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.

Quanto tempo richiede la lezione «Passare dati Mojo a Python»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Mojo Academy?

Sì. Ogni lezione Mojo Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Utilizzare NumPy da Mojo
  2. Passare dati Mojo a Python
  3. Leggere gli oggetti Python restituiti
  4. Insidie prestazionali dell'interoperabilità
← Torna a Mojo Academy