6.11 לינטרים פתרון
הרצת לינטרים¶
אחרי הרצת black ותיקון אזהרות pylint, הקוד הנקי נראה כך:
from typing import Union
def calculate(x: int, y: int, operation: str) -> Union[int, float]:
"""Calculate arithmetic operation on two numbers."""
if operation == "add":
result = x + y
elif operation == "sub":
result = x - y
elif operation == "mul":
result = x * y
elif operation == "div":
result = x / y
else:
raise ValueError(f"Unknown operation: {operation}")
return result
def main() -> None:
x = 10
y = 5
print(calculate(x, y, "add"))
print(calculate(x, y, "div"))
if __name__ == "__main__":
main()
מה תוקן:
- הסרת import os ו-import sys - לא בשימוש (pylint יזהיר על זה).
- שינוי שם Calculate ל-calculate - קונבנציה של פייתון: שמות פונקציות ב-snake_case.
- הוספת type hinting ו-docstring.
- הוספת raise ValueError למקרה שהפעולה לא מוכרת.
- עטיפת הקוד הראשי ב-main().
- black יוסיף את הרווחים סביב = ואחרי פסיקים אוטמטית.
דיאגרמת מחלקות¶
אין פתרון קוד לתרגיל הזה - הבדקו בעצמכם שהדיאגרמה שנוצרה מתאימה לקוד.