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.
מהו פרדיגמה?
- פרדיגמת תכנות היא סגנון של ארגון תוכנית.
- את אותו בעיה ניתן לפתור בסגנונות שונים.
- Python מאפשר לשלב מספר פרדיגמות בתוך תוכנית אחת.
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.
אימפרטיבית ופרוצדורלית
- קוד אימפרטיבי הוא רשימה של צעדים המשתנים במצב התוכנית.
- הוא אומר איך לבצע פעולה, צעד אחר צעד (לולאות, הקצאות).
- סגנון פרוצדורלי מקבץ צעדים אלו לתוך פרוצדורות ופונקציות הניתנות לשימוש חוזר.
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.
ממוחשבת
- קוד ממוחשב עוטף נתונים ואת השיטות הפועלות עליהם לתוך אובייקטים.
- את הבעיה מדגמים כסט של מחלקות המדברות זו עם זו.
- זה מתאים לתוכניות גדולות כי כל מחלקה היא חלק עצמאי.
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.
הצהרתית
- קוד הצהרתי אומר מה אתה רוצה, ולא איך לחשב זאת.
- השפה או הספרייה מחשבות את הצעדים עבורך.
- דוגמאות: שאילתה בסינת (SQL), או הרצפת רשימה ב-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.
רמת נמוכה
- קוד רמת נמוכה קרוב למכונה: אסמבלי וקוד מכונה.
- מעבד (CPU) מיישן אותו ישירות, ולכן הוא מהיר אך קשה לכתיבה.
- שפות רמה גבוהה כמו Python מתורגמות אליו לפני ההרצה.
Common mistakes
- Procedural is steps; object-oriented is objects with data and methods; declarative describes the result.
- Match the paradigm to the problem.
טעויות נפוצות
- פרוצ'דורלי הם שלבים; אובייקטי-אופנת היא אובייקטים עם נתונים ומתודות; דקלראטיבי מתאר תוצאה.
- התאימו את הפאראדיגמה לבעיה.
Now you try
- Each task solves a small problem in a different paradigm.
- Press Check answer to test your code.
כעת תנסו בעצמכם
- כל משימה פותרת בעיה קטנה בפאראדיגמה שונה.
- לחץ על בדוק תשובה כדי לבדוק את הקוד שלך.
Procedural style. Write sum_list(nums) that uses a loop and an accumulator to add up the list and return the total. · סגנון פרוצ'דורלי. כתוב sum_list(nums) שמשתמש בטבעה ובצובר (accumulator) כדי לסכום את הרשימה ולהחזיר את הסך.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Declarative style. Write evens(nums) that returns a list of the even numbers, using a single list comprehension [... for ... if ...]. · סגנון הצהרתי. כתוב evens(nums) שחזר רשימה של המספרים הזוגיים, באמצעות ביטוי רשימה (list comprehension) יחיד [... for ... if ...].
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
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. · סגנון ממוחשב-אובייקטים. כתוב כיתה Bag שבה ה-__init__(self, items) אוספת רשימה, ושימתה total(self) חזרה סכום הפריטים הללו.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.