Programming paradigms · รูปแบบการเขียนโปรแกรม (Programming paradigms)
What is a paradigm?
- A programming paradigm is a style of organising a program.
- The same problem can be solved in different styles.
- Python lets you mix several paradigms in one program.
Paradigm คืออะไร?
- Programming paradigm เป็นรูปแบบการจัดโครงสร้างโปรแกรม
- ปัญหาเดียวกันสามารถแก้ได้ด้วยสไตล์ต่างๆ
- Python อนุญาตให้คุณผสมผสานหลาย paradigms ในโปรแกรมเดียวได้
Imperative and procedural
- Imperative code is a list of steps that change the program's state.
- It says how to do something, step by step (loops, assignments).
- Procedural style groups those steps into reusable procedures and functions.
Imperative และ Procedural
- โค้ดแบบ Imperative คือรายการขั้นตอนที่เปลี่ยนสถานะของโปรแกรม
- มันบอก วิธีการ ทำสิ่งนั้นเป็นขั้นตอนๆ (loops, assignments)
- สไตล์ Procedural จัดกลุ่มขั้นตอนเหล่านั้นเข้าเป็น procedures และ functions ที่นำกลับมาใช้ใหม่ได้
def total(nums):
result = 0
for n in nums:
result = result + n
return result
print(total([2, 4, 6]))
Object-oriented
- Object-oriented code bundles data and the methods that act on it into objects.
- You model the problem as a set of classes that talk to each other.
- It suits large programs because each class is a self-contained part.
Object-Oriented (เชิงวัตถุ)
- โค้ดแบบ Object-oriented รวมข้อมูลและ methods ที่ทำงานกับข้อมูลเข้าด้วยกันใน objects
- คุณสร้างโมเดลปัญหาเป็นชุดของ classes ที่สื่อสารกัน
- เหมาะกับโปรแกรมขนาดใหญ่เพราะแต่ละ class เป็นส่วนที่สมบูรณ์ในตัวเอง
class Counter:
def __init__(self):
self.count = 0
def add(self):
self.count = self.count + 1
c = Counter()
c.add()
c.add()
print(c.count)
Declarative
- Declarative code says what you want, not how to compute it.
- The language or library works out the steps for you.
- Examples: a database query (SQL), or a Python list comprehension.
Declarative (เชิงคำอธิบาย)
- โค้ดแบบ Declarative บอกว่าคุณต้องการ อะไร ไม่ใช่วิธีการคำนวณ
- ภาษาหรือ library จะหาขั้นตอนให้คุณ
- ตัวอย่าง: การ query ฐานข้อมูล (SQL), หรือ list comprehension ของ Python
nums = [1, 2, 3, 4, 5]
print(sum(nums)) # say WHAT: the total
print([n * n for n in nums]) # a list comprehension
Low-level
- Low-level code is close to the machine: assembly and machine code.
- The CPU runs it directly, so it is fast but hard to write.
- High-level languages like Python are translated down to it before running.
Low-Level (ระดับต่ำ)
- โค้ดแบบ Low-level ใกล้เคียงกับเครื่อง: assembly และ machine code
- CPU รันตรงๆ ดังนั้นจึงเร็วแต่เขียนยาก
- ภาษา High-Level เช่น Python ถูกแปลงเป็นมันก่อนรัน
Common mistakes
- Procedural is steps; object-oriented is objects with data and methods; declarative describes the result.
- Match the paradigm to the problem.
ข้อผิดพลาดที่พบบ่อย
- Procedural คือขั้นตอน; Object-Oriented คือ objects ที่มีข้อมูลและ methods; Declarative อธิบายผลลัพธ์
- เลือกใช้ paradigm ให้ตรงกับปัญหา
Now you try
- Each task solves a small problem in a different paradigm.
- Press Check answer to test your code.
ลองดูเลย
- แต่ละงานแก้ปัญหาเล็กๆ ด้วย paradigm ที่ต่างกัน
- กด Check answer เพื่อทดสอบโค้ดของคุณ
Procedural style. Write sum_list(nums) that uses a loop and an accumulator to add up the list and return the total. · Procedural style. เขียน sum_list(nums) ที่ใช้ loop และ accumulator เพื่อบวกรายการทั้งหมดและ return ผลรวม
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Declarative style. Write evens(nums) that returns a list of the even numbers, using a single list comprehension [... for ... if ...]. · Declarative style. เขียน evens(nums) ที่ return รายการของเลขคู่ โดยใช้ list comprehension [... for ... if ...] เพียงครั้งเดียว
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Object-oriented style. Write a class Bag whose __init__(self, items) stores a list, and whose method total(self) returns the sum of those items. · Object-oriented style. เขียนคลาส Bag ที่มี __init__(self, items) เก็บรายการไว้ และมี method total(self) ที่ return ผลรวมขององค์ประกอบเหล่านั้น
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่