1

I try to understand what assert it is. Every answer say it is debugging stuff. I confused. Should not we use it in production? Should we remove it after the developlement? If assertions are disabled in the Python interpreter. Asserts will be ignored and that can become a security risk? We just use it try to make easy debug on development?

if it is for production. below example i used assert to control not having negative value. Why don't i use if else instead assert?

Thank you in advance.

def get_age(age):
   assert age > 0, "Age can't be negative!"
   print("your age is"+age)
Oğuzhan
  • 91
  • 2
  • 9
  • I like to use assertions in production code, but there are those that advise against it, as assertions are disabled when running Python in optimized mode (with `-O`). – L3viathan Jul 11 '19 at 14:00
  • 1
    This is may or may not be a good use of `assert`. If the `age` parameter is generated internally and it is negative, then yes, you have a problem that should be fixed before release. If `age` is coming from the user, you should not use `assert`. Instead, tell the user the error and ask again. – 001 Jul 11 '19 at 14:00
  • `assert age > 0, "Age can't be negative!"` is short for `if not(age > 0): raise AssertionErorr("Age can't be negative!")` if python is not run with the `-O` argument. The use cases are versatile. It just depends on how you are handling the exception. If you don't, then it might not be a great idea in production. – Klaus D. Jul 11 '19 at 14:00
  • Possible duplicate of [Best practice for Python assert](https://stackoverflow.com/questions/944592/best-practice-for-python-assert) – mkrieger1 Jul 11 '19 at 14:04
  • 1
    @mkrieger1 it does not tell me at all whether it is for production or development or something. Please read my question carefully and make compare between there questions. @ – Oğuzhan Jul 11 '19 at 14:09
  • Nobody can tell you what this particular assertion was *intended* for except the person who wrote it. – mkrieger1 Jul 11 '19 at 14:10
  • (Tangentially, it's not clear what `age` is supposed to be. If it's a numeric type, the string concatenation will fail. If it's a `str`, the comparison will either return a useless boolean value (Python 2) or raise its own error (Python 3).) – chepner Jul 11 '19 at 14:15

2 Answers2

2

If nothing else, you lose control over the behavior of get_age using assertions, because they can be disabled at runtime without modifying the code itself. For production usage, be more explicit and raise a ValueError (or, if you deem it worth the effort, a custom subclass of ValueError):

def get_age(age):
   if age <= 0:
       raise ValueError("Age can't be nonpositive!")
   print("your age is" + str(age))
chepner
  • 497,756
  • 71
  • 530
  • 681
  • if i use assert in production i should not use -o parametre? is this -o parameter vital? if i dont use it, what type of problem i can reach? And lastly i think i should use it development and not use it production. – Oğuzhan Jul 11 '19 at 14:14
  • 1
    Assertions are used to debug things that you *know* shouldn't be true for the remaining code to work. In this case, a negative value for `age` may not be expected, but it won't cause any errors in the following call to `print`. – chepner Jul 11 '19 at 14:16
-1

Assert Statements are used when:

  • the final code is in theory fail safe
  • the current state of the code is not yet fail safe

E.g. You programm a game with a moving car. Let's say you have a really complicated move() function that works absolutely fine for positive input but in special cases bugs out in weird ways for negative input.

You know this function is going to be fine in the final stage of your game as no function part of the game will every call move() with negative input.

But as you are currently still working on an A.I. driving the car so that you can race against bots - you cannot gurantee that you didn't make a mistake and the A.I. calls the move() function with negative input.

Therefore you put an assert input_arg >= 0 at the beginning of your move() function so that you get notified everytime a wrong input arg was given. try-except will not catch this as your movement only bugs out but does not raise an exception.

So the assert input_arg >= 0, 'input_arg must be positive!' is just shorthand for if not (input_arg >= 0): raise ValueError('input_arg must be positive!') while signaling that this is not an actual error that can occur in the final stage of the code but only while developing other parts of the game.

It is shorter, can be differentiated from 'real' errors and therefore also automatically stripped away for production code.

Dames
  • 776
  • 3
  • 11