Hacker News new | comments | show | ask | jobs | submit login

Well, I have to agree with the general point here, but it's buried inside a mass of wandering prose, and I'm not even sure the author quite gets it.

Here is (IMHO) the point: assert is for checking invariants.

That said, I doubt that a post like this is a good place to teach people what invariants are. Those who don't know should go learn. To some small extent, that might include the author, as, for example, pre- and post-conditions are not some special contract-thing that is separate from invariants; they are special kinds of invariants. (So, yes, use assert to check contracts; that's part of checking invariants)

And:

> You wouldn't write code like this:

  if not isinstance(x, int):
      raise AssertionError("not an int")
Sure I would, if x being an instance of int is an invariant of my code. But if it isn't, then I wouldn't.

I also accept that, as the author says, "assert" has its quick-and-dirty uses. Putting an "assert False" around an unwritten portion of code is a reasonable thing to do, particularly if it is code that one expects to be used often, as the "assert" will continually complain of its own existence as long as it is there.




I agree with what you say, but just an aside: I prefer to raise NotImplementedError for unwritten portions of code personally.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: