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