การเรียกฟังก์ชัน Python
เรียกใช้โค้ด Python และอ่านผลลัพธ์
การเรียกฟังก์ชัน Python เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเรียกฟังก์ชัน Python”
เรียกใช้โค้ด Python และอ่านผลลัพธ์ คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การเรียกฟังก์ชัน Python” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม
ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การนำเข้าโมดูล Python
- การเรียกฟังก์ชัน Python
- การย้ายข้อมูลข้ามสะพานเชื่อม
- ควรใช้การทำงานร่วมกับ Python เมื่อใด