0Pricing
Mojo Academy · บทเรียน

การส่งข้อมูล Mojo ไปยัง Python

ส่งค่าข้ามสะพานการทำงานร่วมกัน

การส่งข้อมูล Mojo ไปยัง Python เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.14

Wrap 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 = 10

Strings 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 = 5

Mind 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การส่งข้อมูล Mojo ไปยัง Python”

ส่งค่าข้ามสะพานการทำงานร่วมกัน คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การส่งข้อมูล Mojo ไปยัง Python” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม

ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การใช้ NumPy จาก Mojo
  2. การส่งข้อมูล Mojo ไปยัง Python
  3. การอ่านออบเจ็กต์ Python กลับมา
  4. จุดที่ต้องระวังด้านประสิทธิภาพของการทำงานร่วมกัน
← กลับไปที่ Mojo Academy