MojoのデータをPythonに渡す
相互運用ブリッジを通じて値を渡します
「MojoのデータをPythonに渡す」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 🎯
よくある質問
「MojoのデータをPythonに渡す」レッスンは無料ですか?
はい。「MojoのデータをPythonに渡す」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「MojoのデータをPythonに渡す」で何を学びますか?
相互運用ブリッジを通じて値を渡します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「MojoのデータをPythonに渡す」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- MojoからNumPyを使う
- MojoのデータをPythonに渡す
- Pythonオブジェクトを読み戻す
- 相互運用におけるパフォーマンスの注意点