HOW You Should Be Using ASSERT In Python To Catch ERRORS

preview_player
Показать описание
Let's find out why & how you should be using the assert keyword in Python.

▶ Become job-ready with Python:

▶ Follow me on Instagram:
Рекомендации по теме
Комментарии
Автор

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.

caiolaytynher
Автор

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

wartem
Автор

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

bassycounter
Автор

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.

RedShipsofSpainAgain
Автор

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.

FrankSiler
Автор

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?

MustbeTheBassest
Автор

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.

JongJong-ocel
Автор

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?

tom_stephen
Автор

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


assert dominance

yourhardworkgone
Автор

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

renataalmiratoth
Автор

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!

Prae
Автор

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

renataalmiratoth
Автор

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? \n'
l = 'I am the guy who loves Python '
print(f"mixed numbers result is: \n{mix(var1=a, var2=b)}, random mixer number is {rl}")
print(f"combined text result is: \n{combine(strvl1=n, strvl2=l)}")

how i share you the code I don't know

AstronomyAstropy
Автор

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"

minecraft
Автор

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

xushenxin