2

is it bad practice to use asserts within methods?

e.g.

def add(x, y):
    assert isinstance(x, int) and isinstance(y, int)
    return x + y

Any ideas?

4 Answers 4

6

Not at all.

In your sample, provided you have documented that add expects integers, asserting this constraint at the beginning of the method is actually great practice.

Just imagine the other choices you have and how bad they are:

  • don't verify your arguments. This means, the method will fail later with a strange backtrace that will presumably confuse the caller and force him to have a look at the implementation of add to get a hint what's going on.
  • be nice and try to convert the input to int - very bad idea, users will keep wondering why add(2.4,3.1) keeps returning 5.
Sign up to request clarification or add additional context in comments.

8 Comments

In a case like this, wouldn't you prefer to raise an exception instead? I prefer to use asserts only when debugging code.
it surely depends on the occasion (especially in dynamically-typed languages), but exceptions are for runtime errors while assertions are used to catch programming errors. If the docs for add clearly state that it only accepts 'int', then it is the responsibility of the caller to keep to this constraint and any contract violation is a programming error.
(Besides, assert in Python raises AssertionError - so it is actually syntatic sugar to raise an exception).
Yes, it raises an AssertError. IMHO, in this case, I'd simply raise TypeError instead.
But semantically, an assertion means "this cannot possibly be false". Invalid arguments are very possible and should raise an appropriate error (e.g. TypeError) that indicates "some data is wrong" instead of "this code is broken" - also, I consider except AssertionError (outside of tests perhaps) rather smelly for that very reason.
|
2

It's ok because you may run your application with -O command line option and no code would be generated for your assert statement see here

Update:

But also you should handle all errors anyway. Otherwise after stripping assertions unhandled exceptions may occur. (as McConnell recomended. See his citations here)

5 Comments

Why is that an argument in favor of asserts?
I mean asserting is good practice, so the question is whether there are drawbacks in case of python scripts. The bad thing would be assertion exceptions appearing when your script is running on "release" mode. However, python allows you to avoid this.
But code such as OP's uses asserts for argument validation - it may carry on with wrong/bogus values and crash later on if asserts are skipped.
I agree with McConnell in Thomas Owens's answer here: stackoverflow.com/questions/17732/…
Then add that to the answer. It's important. Without the "handle the error anyway" part, stripping the assertions leads to incorrect code.
0

It's not but if your code contains more assert statements than your actual code then I would be angry.

Comments

0

Instead of using assertions and raising Assertion exception...better perform proper checks using instance() and raise a proper TypeError.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.