נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
PythonPython

דיבאג עם VS CodeDebugging with VS Code

מפגש 14: נקודות עצירה, צעד-אחר-צעד, צפייה במשתנים ושיטה למציאת באגים.Session 14: Breakpoints, stepping, watching variables and a method for finding bugs.

מה נעשה היוםToday's plan

  • שיטהMethod
  • דיבאגרDebugger
  • הדפסותPrints
  • assertassert
  • logginglogging
  • באגים קלאסייםClassic bugs
נושאTopic 1

שיטהMethod

שיטה למציאת באגיםA method for finding bugs

  • לשחזר: מה בדיוק קרה?Reproduce: what exactly happened?
  • לצמצם: איפה בדיוק?Narrow down: where exactly?
  • להשוות ציפייה למציאותCompare expectation and reality

אנלוגיה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.
נושא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

אנלוגיה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.
נושא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
שאלה לכיתה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)=}")

    הדפסות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)=}")
    פלטOutput
    title='Dune ', search='Dune'
    False
    'Dune ' 5
    age='30' type(age)=<class 'str'>

    אנלוגיהAnalogy

    הדפסת דיבאג היא כמו להשאיר פתקים לעצמכם לאורך מבוך: "הייתי כאן, והמפתח היה בכיס".A debug print is like leaving notes to yourself through a maze: "I was here, and the key was in my pocket".
    נושאTopic 4

    assertassert

    assert: הנחות שבודקות את עצמןassert: assumptions that check themselves

    • assert תנאי, "הודעה"assert condition, "message"
    • נכשל מוקדם ובקולFails early and loudly
    • הבסיס של בדיקות (מפגש 19)The basis of tests (session 19)

    אנלוגיהAnalogy

    assert הוא כמו נורת "חגורה לא חגורה" ברכב: לא מחכה לתאונה כדי להודיע שמשהו לא בסדר.assert is like the car's seatbelt warning light: it doesn't wait for a crash to tell you something's wrong.
    נושאTopic 5

    logginglogging

    logging: הדפסות שאפשר לכבותlogging: prints you can switch off

    • רמות: DEBUG, INFO, WARNING, ERRORLevels: DEBUG, INFO, WARNING, ERROR
    • basicConfig(level=...) קובע מה מוצגbasicConfig(level=...) decides what shows
    • לא צריך למחוק כשמסיימיםNo need to delete when done

    אנלוגיהAnalogy

    logging הוא כמו רמקול עם בורר ערוצים: בזמן חזרות שומעים הכול, ובהופעה רק הודעות חשובות.logging is like a speaker with a channel selector: in rehearsal you hear everything, at the show only the important messages.
    נושאTopic 6

    באגים קלאסייםClassic bugs

    באגים קלאסייםClassic bugs

    • off-by-one: range(1, n) חסר את nOff-by-one: range(1, n) misses n
    • = מול ==, הזחה שגויה= vs ==, wrong indentation
    • שיטת הברווז: להסביר בקולRubber duck: explain it out loud

    אנלוגיהAnalogy

    שיטת הברווז עובדת כי כשמסבירים, המוח עובר מ"מה שהתכוונתי" ל"מה שכתוב בפועל".The duck works because explaining moves your brain from "what I meant" to "what's actually written".

    לשחזר: מה בדיוק קרה?Reproduce: what exactly happened?

    לצמצם: איפה בדיוק?Narrow down: where exactly?

    להשוות ציפייה למציאותCompare expectation and reality

    נקודת עצירה: לחיצה ליד מספר השורהBreakpoint: click by the line number

    F5 מתחיל, F10 צעד, F11 נכנס לפונקציהF5 starts, F10 steps, F11 steps in

    חלון Variables מראה הכולThe Variables pane shows everything

    print(f"{x=}") מדפיס שם וערךprint(f"{x=}") prints name and value

    repr מראה רווחים ומירכאותrepr shows spaces and quotes

    type() כשחושדים בטיפוסtype() when you suspect a type

    assert תנאי, "הודעה"assert condition, "message"

    נכשל מוקדם ובקולFails early and loudly

    הבסיס של בדיקות (מפגש 19)The basis of tests (session 19)

    רמות: DEBUG, INFO, WARNING, ERRORLevels: DEBUG, INFO, WARNING, ERROR

    basicConfig(level=...) קובע מה מוצגbasicConfig(level=...) decides what shows

    לא צריך למחוק כשמסיימיםNo need to delete when done

    off-by-one: range(1, n) חסר את nOff-by-one: range(1, n) misses n

    מה למדנוWhat we learned

    • לשחזר: מה בדיוק קרה?Reproduce: what exactly happened?
    • נקודת עצירה: לחיצה ליד מספר השורהBreakpoint: click by the line number
    • print(f"{x=}") מדפיס שם וערךprint(f"{x=}") prints name and value
    • assert תנאי, "הודעה"assert condition, "message"
    • רמות: DEBUG, INFO, WARNING, ERRORLevels: DEBUG, INFO, WARNING, ERROR
    • off-by-one: range(1, n) חסר את nOff-by-one: range(1, n) misses n

    עכשיו לתרגולNow, practice

    5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
    הערות מרצהSpeaker notes

    נבנה על ידיBuilt by Ariel Yohimovich