ブリッジをまたいだデータ移動
MojoとPythonの値を相互変換します
「ブリッジをまたいだデータ移動」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Two Worlds of Values
Mojo has its own native types, and Python has its own. Interop means converting values as they cross between the two worlds.
Mojo to Python
When you pass a Mojo value into a Python call, Mojo automatically wraps it into a PythonObject Python can understand.
var n: Int = 42
print(np.sqrt(n))Numbers Convert Cleanly
Simple values like Int and Float move across smoothly, becoming Python ints and floats with no extra work from you.
var x: Float64 = 3.5
var r = math.floor(x)Strings Travel Too
A Mojo String becomes a Python str on the way in, so text-based functions just work across the bridge.
var name = String("Mojo")
print(name)Python to Mojo
Coming back, Python results arrive as a PythonObject. You then convert it into a native Mojo type when you need one.
var obj = math.factorial(5)Unwrap to Int
To get a real Mojo number from a result, construct the type from the object, like wrapping it in Int.
var count = Int(math.factorial(5))Unwrap to Float
The same idea works for decimals: build a Float64 from the Python result to use it in Mojo math.
var ratio = Float64(math.pi)Unwrap to String
Turn a Python text result into a Mojo String the same way, so you can store and process it natively.
var label = String(obj)Indexing Python Collections
You can index a Python list or array result with brackets, getting back another PythonObject to convert.
var first = arr[0]Convert at the Edges
A good habit: keep values as PythonObjects while talking to Python, then convert to Mojo types at the boundary you control.
Why It Matters
Knowing how data crosses the bridge keeps your types predictable, so Python results flow safely into fast Mojo code. 🌉
Quick Check
Recall how to turn a Python result into a Mojo number.
Recap
You saw Mojo values become PythonObjects going in, and learned to convert results back into Int, Float, or String. 🎯
よくある質問
「ブリッジをまたいだデータ移動」レッスンは無料ですか?
はい。「ブリッジをまたいだデータ移動」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「ブリッジをまたいだデータ移動」で何を学びますか?
MojoとPythonの値を相互変換します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「ブリッジをまたいだデータ移動」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Pythonモジュールのインポート
- Python関数の呼び出し
- ブリッジをまたいだデータ移動
- Python相互運用を使う場面