Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Rob Gaddi Newsgroups: comp.lang.python Subject: Re: I love assert Date: Tue, 11 Nov 2014 12:20:37 -0800 Organization: Highland Technology, Inc. Lines: 25 Message-ID: <20141111122037.6753b57f@rg.highlandtechnology.com> References: <96c138e4-ab95-4e5f-a0c8-e696b5a926bb@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Injection-Info: mx02.eternal-september.org; posting-host="a48e804ab85e47f9d2e8748775b64aeb"; logging-data="15592"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX181nH+62svy1t1ALUngkM36" X-Newsreader: Claws Mail 3.9.3 (GTK+ 2.24.23; x86_64-pc-linux-gnu) Cancel-Lock: sha1:ed+HCUQvCQvA1JBZosxBWd7L4AY= Xref: csiph.com comp.lang.python:80559 On Tue, 11 Nov 2014 11:40:38 -0800 (PST) Peter Cacioppi 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.