HOW You Should Be Using ASSERT In Python To Catch ERRORS

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ธ.ค. 2024

ความคิดเห็น • 32

  • @caiolaytynher5994
    @caiolaytynher5994 2 ปีที่แล้ว +5

    Great content, I started using assert recently learning how to unittest, and never came into my mind that it can be used in this way, even allowing us to speed code in production. Thanks dude, you're awesome.

  • @wartem
    @wartem 2 ปีที่แล้ว +2

    Really great that you also talked about how assertions affect code performance and how to disable them in production. Concise yet informative.

  • @barnabykent6698
    @barnabykent6698 18 วันที่ผ่านมา

    Very clear for a beginner. Excellent content to noise ratio. Thank you.

  • @bassycounter
    @bassycounter ปีที่แล้ว +2

    I didn't know about the __debug__ and using -O and -OO flags, that's pretty handy

  • @RedShipsofSpainAgain
    @RedShipsofSpainAgain ปีที่แล้ว +2

    3:55 btw, it's probably not a good idea to name your json object "json", as that is just inviting trouble from confusion with the keyword also named json.
    Instead, name it something more descriptive like json_data, or even json_obj. But not just json.

  • @yourhardworkgone
    @yourhardworkgone 2 ปีที่แล้ว +3

    try..except..finally be like: hey man
    assert dominance

  • @MustbeTheBassest
    @MustbeTheBassest 2 ปีที่แล้ว +4

    Wait, I'm confused. This wouldn't be used in production code? Then how do you check inputs in production? I'm assuming you should?

    • @maxthexpfarmer3957
      @maxthexpfarmer3957 2 ปีที่แล้ว

      yeah don't use it in production

    • @MustbeTheBassest
      @MustbeTheBassest 2 ปีที่แล้ว

      @@maxthexpfarmer3957 thanks, could you give any more details? Do you not check inputs in production?

    • @maxthexpfarmer3957
      @maxthexpfarmer3957 2 ปีที่แล้ว +1

      @@MustbeTheBassest I'm just a student, but this is how I would do it:
      data=input(`prompt`):
      while not `validation condition`:
      print(`error message`)
      data=input(`prompt`)
      #more code goes here

    • @caiolaytynher5994
      @caiolaytynher5994 2 ปีที่แล้ว +2

      In production, you always have the right input because you've wrote it in a way that it does not raise any exceptions.
      That is different from user input though, cuz you cannot be certain that the input is right.
      If you called a function that you've written inside your own code, and it doesn't crash it, then why you need to check again?
      Hope that is helpful.

    • @caiolaytynher5994
      @caiolaytynher5994 2 ปีที่แล้ว +2

      @@maxthexpfarmer3957 yeah, that right there is user input, and that always have to be checked, but the case explained in the video was about input passed in to a function inside the code, in a way that can't even be looped through. That's a thing that I also realized recently.

  • @FrankSiler
    @FrankSiler 2 ปีที่แล้ว +3

    Good overview. The only thing I would worry about is that you keep referencing JSON. Python does not have a JSON type per se- in fact, you're using dict, which is natural, but I worry that folks just learning will get confused about the types and data flow.

    • @Indently
      @Indently  2 ปีที่แล้ว +1

      Thanks for the constructive criticism, I mess up the names a lot when recording, even if I know exactly the difference, someone without experience might get confused, you're right.

  • @JongJong-oc1el
    @JongJong-oc1el 7 หลายเดือนก่อน

    question.
    you use assert to check the assumption of the value is the right type as what i understood from your code. I am still beginner but my question if we can use assert for that way. Then i can use it in replacement for if...else in python?
    It looks better compared to using if..else and print according to the condition.
    what do you think with what understood from looking at the code in your video.
    I really thought assert() is part of unittest framework or from pytest.

  • @tom_stephen
    @tom_stephen 2 ปีที่แล้ว

    So, if I could try to reconceptualise the lesson, `assert` advantage over `raiseError` is that you can turn it off through non-debug mode, while the latter would still run? Did I understand this correctly?
    And is there any more advantages to `assert` over second way?

  • @РоБ-с8л
    @РоБ-с8л 10 หลายเดือนก่อน

    So clear, thx

  • @johncarrprogramming62
    @johncarrprogramming62 ปีที่แล้ว

    Great explanation!

  • @GooogleGoglee
    @GooogleGoglee 2 ปีที่แล้ว

    Grazie Fede! Molto utile!

  • @Jerrinth3glitch
    @Jerrinth3glitch ปีที่แล้ว

    definitely awesome

  • @renataalmiratoth3110
    @renataalmiratoth3110 ปีที่แล้ว

    its good at all but in my screen the error is name not being defined so my project is not gonna work

  • @P1ra8e
    @P1ra8e 2 ปีที่แล้ว

    Great video!, Can you provide me on a road map to encode my python code and add a licensing system into it? Thanks in advance!

  • @renataalmiratoth3110
    @renataalmiratoth3110 ปีที่แล้ว

    i mean how come u can add a variable witouth even defining it?!

    • @jyvben1520
      @jyvben1520 ปีที่แล้ว

      timestamp would be nice, where in the video is that variable ?
      are you complaining about the message "invalid JSON", he should have said dictionary,
      as the parameter said data is type dictionary.

  • @BijouBakson
    @BijouBakson ปีที่แล้ว

    Thank you

  • @grzegorzryznar5101
    @grzegorzryznar5101 2 ปีที่แล้ว

    Nice tric with this OO :)

  • @AstronomyAstropy
    @AstronomyAstropy 2 ปีที่แล้ว +1

    Hi Indently, thanks I learned lot of things and I made my own functions, Combine and Mix code here:
    import random
    rl = random.randint(3, 4)
    def mix(var1, var2):
    return var1 + var2 + rl
    def combine(strvl1, strvl2):
    return str(strvl1+strvl2)
    a = 12
    b = 7
    n = 'Hello, bro what's your name?
    '
    l = 'I am the guy who loves Python '
    print(f"mixed numbers result is:
    {mix(var1=a,var2=b)} , random mixer number is {rl}")
    print(f"combined text result is:
    {combine(strvl1=n,strvl2=l)}")
    how i share you the code I don't know

  • @1234minecraft5678
    @1234minecraft5678 5 หลายเดือนก่อน

    i dont believe that the presented use case for json is all that usefull. in my opinion you should build a dataclass and parse the json right into it. they you will know whether the input data fulfills your expectations. you might want to use a library called dacite to parse the json object to the dataclass. this way, you already covered all of the mentioned asserts regarding jsonl.
    there is a great article about this, which is called "parse dont validate"

  • @xushenxin
    @xushenxin ปีที่แล้ว

    can you talk less? I feel really don't like to here people talking, just the content.

    • @Indently
      @Indently  ปีที่แล้ว +5

      I'm scared to think of how you talk to strangers in real life mate 😅