HOW You Should Be Using ASSERT In Python To Catch ERRORS

  Рет қаралды 18,995

Indently

Indently

Күн бұрын

Пікірлер: 32
@caiolaytynher5994
@caiolaytynher5994 2 жыл бұрын
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 жыл бұрын
Really great that you also talked about how assertions affect code performance and how to disable them in production. Concise yet informative.
@RedShipsofSpainAgain
@RedShipsofSpainAgain Жыл бұрын
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.
@bassycounter
@bassycounter Жыл бұрын
I didn't know about the __debug__ and using -O and -OO flags, that's pretty handy
@barnabykent6698
@barnabykent6698 26 күн бұрын
Very clear for a beginner. Excellent content to noise ratio. Thank you.
@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.
@MustbeTheBassest
@MustbeTheBassest 2 жыл бұрын
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 жыл бұрын
@@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 жыл бұрын
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 жыл бұрын
@@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.
@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?
@FrankSiler
@FrankSiler 2 жыл бұрын
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 жыл бұрын
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.
@yourhardworkgone
@yourhardworkgone 2 жыл бұрын
try..except..finally be like: hey man assert dominance
@РоБ-с8л
@РоБ-с8л 11 ай бұрын
So clear, thx
@johncarrprogramming62
@johncarrprogramming62 Жыл бұрын
Great explanation!
@GooogleGoglee
@GooogleGoglee 2 жыл бұрын
Grazie Fede! Molto utile!
@renataalmiratoth3110
@renataalmiratoth3110 Жыл бұрын
its good at all but in my screen the error is name not being defined so my project is not gonna work
@Jerrinth3glitch
@Jerrinth3glitch 2 жыл бұрын
definitely awesome
@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
@Indently
@Indently Жыл бұрын
Welcome!
@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!
@grzegorzryznar5101
@grzegorzryznar5101 2 жыл бұрын
Nice tric with this OO :)
@AstronomyAstropy
@AstronomyAstropy 2 жыл бұрын
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 Жыл бұрын
I'm scared to think of how you talk to strangers in real life mate 😅
WHAT Is "Glob" In Python?! (It's Actually Very Useful!)
11:15
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН
5 Good Python Habits
17:35
Indently
Рет қаралды 665 М.
python: what is assert? (beginner) anthony explains #240
8:39
anthonywritescode
Рет қаралды 24 М.
JS Fetch in 3 Minutes
3:30
CodeNexus
Рет қаралды 402
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 819 М.
Learn Python's AsyncIO in 15 minutes
14:02
Indently
Рет қаралды 53 М.
6 CLEAN Tips To IMPROVE Your Python Functions
9:55
Indently
Рет қаралды 83 М.
The True Power of "ChainMap" in Python
13:04
Indently
Рет қаралды 21 М.
Modern Graphical User Interfaces in Python
11:12
NeuralNine
Рет қаралды 1,6 МЛН
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
Python dataclasses will save you HOURS, also featuring attrs
8:50
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН