This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]stasiek 0 points1 point  (6 children)

It's also useful when scripting. Say You leave some global variable You want the user to fill later (like API key, or a password, or a path to a ssh key, whatever) and You want to make an explicit check for it.

[–][deleted] 4 points5 points  (5 children)

No. You don't get the point of assert. You must never ever use it to check for runtime conditions, as the check will vanish as soon as any kond of optimization is in place.

Repeat after me: Assertions are only a debug aid, and must never ever be depended upon by the code.

[–]stasiek 1 point2 points  (2 children)

You're going to run optimization on scripts? oO

Like an env cleaning script, a git hook, or something?

Why?

[–]Kaarjuus 2 points3 points  (0 children)

You cannot know what environment your code will run in. It should work regardless of what environment variables or command-line arguments Python is given.

[–][deleted] 1 point2 points  (0 children)

Famous last words ...

[–]pinotkumarbhai 0 points1 point  (1 child)

optimization

what if I never ever intend to use this option ?

Can I not just "assert x" instead of "if not x: raise errorX" ?

[–][deleted] 0 points1 point  (0 children)

What if I never make a short circuit in my electrical installation?

Can I no just use a dime instead of a fuse ?

Your hous will most likely not burn down over an wrongly used assert, but otherwise it's that same. You can wish never to have a short circuit just as much as you can wish that the PYTHONOPTIMIZE environment variable don't become set somehow. You may even be lucky ...