Chiamare funzioni Python
Evochi codice Python e legga i risultati.
Chiamare funzioni 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.
From Import to Action
Importing a module is just step one. The real payoff is calling its functions and getting results back into your Mojo program.
Call With Dots
Use the module handle and dot syntax to call a function, exactly like in Python: module name, dot, function, parentheses.
var root = math.sqrt(16)Pass Arguments Normally
Send arguments inside the parentheses just as you would in Python. Mojo forwards them across the bridge for you.
var biggest = builtins.max(3, 9, 5)Results Are PythonObjects
A Python call returns a PythonObject, a flexible wrapper that can hold any Python value the function produced.
var result = np.add(2, 3)Print It Directly
You can print a PythonObject straight away. Mojo asks Python for its text form, so output looks just like in Python.
print(np.array([1, 2, 3]))Chain Method Calls
Because results are Python objects, you can call methods on them too, chaining calls just like fluent Python code.
var text = greeting.upper()Keyword Arguments Work
Named arguments carry over the bridge as well, so you can call Python functions with keyword arguments by name.
var a = np.zeros(5, dtype=np.int32)Calls Can Raise
A Python function might throw, so calling it can raise in Mojo. Put such calls in a function marked with raises.
fn use() raises:
var r = math.sqrt(2)Access Attributes Too
You are not limited to functions. Read module attributes and constants the same way, with simple dot access.
var tau = math.tauFull Library Power
Calling Python functions means the entire API of a library is available, letting you orchestrate rich logic from Mojo.
Why It Matters
You can drive proven Python code from Mojo, mixing convenient libraries with your own fast Mojo logic in one program. 🚀
Quick Check
Recall what a Python function call gives back in Mojo.
Recap
You called Python functions with dot syntax, passed arguments, and received a PythonObject result you can print or reuse. 🎯
Domande Frequenti
La lezione «Chiamare funzioni Python» è gratuita?
Sì — il testo completo di «Chiamare funzioni 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 «Chiamare funzioni Python»?
Evochi codice Python e legga i risultati. 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 «Chiamare funzioni 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
- Importare un modulo Python
- Chiamare funzioni Python
- Trasferire dati attraverso il bridge
- Quando usare l'interoperabilità con Python