I love assert

Rob Gaddi rgaddi at technologyhighland.invalid
Tue Nov 11 21:20:37 CET 2014

On Tue, 11 Nov 2014 11:40:38 -0800 (PST)
Peter Cacioppi <peter.cacioppi at gmail.com> wrote:

> I get the impression that most Pythonistas aren't as habituated with assert statements as I am. Is that just a misimpression on my part? If not, is there a good reason to assert less with Python than other languages?
> As far as I can tell, Python supports assert perfectly well. When run with the optimization flagging, the asserts are truly removed.
> I think one needs to take care with some basic assert coding - it's not a substitute for unit tests, it doesn't absolve you of normal exception responsibilities, and, most of all, it should be used for passive inspection and not action. But given these guidelines, I still find it very useful as "active comments".

I tend to be a big fan, but it is easy to forget about the one big rake
assert leaves in your lawn.  You CAN NOT use an assert with any
function that has a side effect or you break everything.

assert test_and_set(var)

That line there is pretty obvious about what's going on, but with the
ability to turn class data members transparently into properties, using
asserts safely actually requires a shocking amount of self-discipline.

assert obj.read_accesses < 10

Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.

More information about the Python-list mailing list