מפגש 14: נקודות עצירה, צעד-אחר-צעד, צפייה במשתנים ושיטה למציאת באגים.Session 14: Breakpoints, stepping, watching variables and a method for finding bugs.
נקודות עצירה, צעד-אחר-צעד, צפייה במשתנים ושיטה למציאת באגים. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Breakpoints, stepping, watching variables and a method for finding bugs. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
שיטהMethod
דיבאגרDebugger
הדפסותPrints
assertassert
logginglogging
באגים קלאסייםClassic bugs
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
נושאTopic 1
שיטהMethod
שיטה למציאת באגיםA method for finding bugs
לשחזר: מה בדיוק קרה?Reproduce: what exactly happened?
לצמצם: איפה בדיוק?Narrow down: where exactly?
להשוות ציפייה למציאותCompare expectation and reality
באג הוא פער בין מה שציפיתם למה שקרה. שיטה בארבעה צעדים: (1) לשחזר את הבאג בכוונה, (2) לצמצם את האזור, (3) להשוות בכל נקודה מה ציפיתם לראות מול מה שיש, (4) לתקן ולבדוק שוב. הקוד הבא מחזיר ממוצע שגוי. איפה?A bug is a gap between what you expected and what happened. A four-step method: (1) reproduce it on purpose, (2) narrow the area, (3) at each point compare what you expected with what's there, (4) fix and check again. This code returns a wrong average. Where?
אנלוגיהAnalogy
לחפש באג זה כמו לחפש נזילה בצנרת: לא מפרקים את כל הבית. סוגרים ברז אחרי ברז, עד שמוצאים את הקטע שבו המים בורחים.Hunting a bug is like finding a leak in plumbing: you don't take the house apart. You close one valve after another until you find the section where water escapes.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 2
דיבאגרDebugger
הדיבאגר של VS CodeVS Code's debugger
נקודת עצירה: לחיצה ליד מספר השורהBreakpoint: click by the line number
F5 מתחיל, F10 צעד, F11 נכנס לפונקציהF5 starts, F10 steps, F11 steps in
חלון Variables מראה הכולThe Variables pane shows everything
הדיבאגר עושה בדיוק מה שהסימולטורים בקורס עושים, אבל על הקוד שלכם: עוצר בשורה ומראה את כל המשתנים. שמים נקודת עצירה (נקודה אדומה משמאל למספר השורה), לוחצים F5 ובוחרים Python File.The debugger does exactly what this course's simulators do, but on your own code: it pauses on a line and shows every variable. Set a breakpoint (a red dot left of the line number), press F5 and choose Python File.
אנלוגיהAnalogy
דיבאגר הוא כמו להריץ סרט בהילוך איטי עם כפתור עצירה: אפשר לעצור בכל פריים ולבדוק מי עומד איפה.A debugger is like playing a film in slow motion with a pause button: you can stop on any frame and check who's standing where.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 3
הדפסותPrints
הדפסות דיבאג חכמותSmart debug prints
print(f"{x=}") מדפיס שם וערךprint(f"{x=}") prints name and value
repr מראה רווחים ומירכאותrepr shows spaces and quotes
type() כשחושדים בטיפוסtype() when you suspect a type
לפעמים הדפסה מהירה עדיפה על דיבאגר. הטריק f"{x=}" מדפיס גם את שם המשתנה וגם את הערך. repr חושף רווחים נסתרים ומירכאות, וכך מגלים למה "Dune " לא שווה ל-"Dune".Sometimes a quick print beats a debugger. The f"{x=}" trick prints both the variable's name and value. repr reveals hidden spaces and quotes, showing why "Dune " doesn't equal "Dune".
שאלה לכיתהQuestion for the class
מה ידפיס הקוד?What will this print?
python
title ="Dune "
search ="Dune"print(f"{title=}, {search=}")
print(title == search)
print(repr(title), len(title))
age ="30"print(f"{age=} {type(age)=}")
נותנים לכיתה חצי דקה לנחש, ואז עוברים לשקופית הבאה.Give the class half a minute to guess, then move on.
הדפסותPrints
prints.py
title ="Dune "
search ="Dune"print(f"{title=}, {search=}")
print(title == search)
print(repr(title), len(title))
age ="30"print(f"{age=} {type(age)=}")
מריצים ומשווים לניחושים.Run it and compare with the guesses.
אנלוגיהAnalogy
הדפסת דיבאג היא כמו להשאיר פתקים לעצמכם לאורך מבוך: "הייתי כאן, והמפתח היה בכיס".A debug print is like leaving notes to yourself through a maze: "I was here, and the key was in my pocket".
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 4
assertassert
assert: הנחות שבודקות את עצמןassert: assumptions that check themselves
assert תנאי, "הודעה"assert condition, "message"
נכשל מוקדם ובקולFails early and loudly
הבסיס של בדיקות (מפגש 19)The basis of tests (session 19)
assert בודק הנחה שאתם בטוחים בה. אם היא שגויה, התוכנית נעצרת מיד עם הודעה, במקום להמשיך עם נתונים שבורים ולהיכשל במקום רחוק ומבלבל. כך עובדות גם הבדיקות האוטומטיות של התרגילים באתר.assert checks an assumption you're sure of. If it's wrong, the program stops at once with a message, instead of carrying on with broken data and failing somewhere far and confusing. The site's exercise checks work this way too.
אנלוגיהAnalogy
assert הוא כמו נורת "חגורה לא חגורה" ברכב: לא מחכה לתאונה כדי להודיע שמשהו לא בסדר.assert is like the car's seatbelt warning light: it doesn't wait for a crash to tell you something's wrong.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 5
logginglogging
logging: הדפסות שאפשר לכבותlogging: prints you can switch off
basicConfig(level=...) קובע מה מוצגbasicConfig(level=...) decides what shows
לא צריך למחוק כשמסיימיםNo need to delete when done
logging הוא כמו print עם מתג עוצמה. כל הודעה מקבלת רמה, וההגדרה קובעת מאיזו רמה מציגים. בזמן פיתוח מציגים DEBUG, ובתוכנית גמורה רק WARNING ומעלה, בלי לגעת בקוד.logging is like print with a volume knob. Each message has a level, and the setting decides from which level to show. During development show DEBUG; in a finished program only WARNING and up, without touching the code.
אנלוגיהAnalogy
logging הוא כמו רמקול עם בורר ערוצים: בזמן חזרות שומעים הכול, ובהופעה רק הודעות חשובות.logging is like a speaker with a channel selector: in rehearsal you hear everything, at the show only the important messages.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 6
באגים קלאסייםClassic bugs
באגים קלאסייםClassic bugs
off-by-one: range(1, n) חסר את nOff-by-one: range(1, n) misses n
כמה באגים חוזרים אצל כולם: טעות באחד בגבולות (off-by-one), return בתוך לולאה במקום אחריה, ושורה בהזחה הלא נכונה. כשנתקעים, שיטת הברווז עובדת להפליא: מסבירים כל שורה בקול, ובאמצע ההסבר הטעות קופצת.A few bugs hit everyone: off-by-one at the edges, a return inside a loop instead of after it, and a line at the wrong indentation. When stuck, rubber duck debugging works wonders: explain each line out loud, and halfway through the mistake jumps out.
אנלוגיהAnalogy
שיטת הברווז עובדת כי כשמסבירים, המוח עובר מ"מה שהתכוונתי" ל"מה שכתוב בפועל".The duck works because explaining moves your brain from "what I meant" to "what's actually written".
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
לשחזר: מה בדיוק קרה?Reproduce: what exactly happened?
שיטה למציאת באגיםA method for finding bugs
לצמצם: איפה בדיוק?Narrow down: where exactly?
שיטה למציאת באגיםA method for finding bugs
להשוות ציפייה למציאותCompare expectation and reality
שיטה למציאת באגיםA method for finding bugs
נקודת עצירה: לחיצה ליד מספר השורהBreakpoint: click by the line number
הדיבאגר של VS CodeVS Code's debugger
F5 מתחיל, F10 צעד, F11 נכנס לפונקציהF5 starts, F10 steps, F11 steps in
הדיבאגר של VS CodeVS Code's debugger
חלון Variables מראה הכולThe Variables pane shows everything
הדיבאגר של VS CodeVS Code's debugger
print(f"{x=}") מדפיס שם וערךprint(f"{x=}") prints name and value
הדפסות דיבאג חכמותSmart debug prints
repr מראה רווחים ומירכאותrepr shows spaces and quotes
הדפסות דיבאג חכמותSmart debug prints
type() כשחושדים בטיפוסtype() when you suspect a type
הדפסות דיבאג חכמותSmart debug prints
assert תנאי, "הודעה"assert condition, "message"
assert: הנחות שבודקות את עצמןassert: assumptions that check themselves
נכשל מוקדם ובקולFails early and loudly
assert: הנחות שבודקות את עצמןassert: assumptions that check themselves
הבסיס של בדיקות (מפגש 19)The basis of tests (session 19)
assert: הנחות שבודקות את עצמןassert: assumptions that check themselves