Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Anything equivalent to cassert in C++?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


pengyu.ut at gmail

Nov 17, 2009, 1:19 PM

Post #1 of 9 (310 views)
Permalink
Anything equivalent to cassert in C++?

There are some assertion code (testing if a condition is false, if it
is false, raise an Error object) in my python, which is useful when I
test my package. But such case would never occur when in the produce
code. If I keep them in if statement, it will take some runtime. I'm
wondering what is the practice that take care of the assertion code in
python.
--
http://mail.python.org/mailman/listinfo/python-list


sajmikins at gmail

Nov 17, 2009, 2:01 PM

Post #2 of 9 (290 views)
Permalink
Re: Anything equivalent to cassert in C++? [In reply to]

On Tue, Nov 17, 2009 at 4:19 PM, Peng Yu <pengyu.ut [at] gmail> wrote:
> There are some assertion code (testing if a condition is false, if it
> is false, raise an Error object) in my python, which is useful when I
> test my package.  But such case would never occur when in the produce
> code. If I keep them in if statement, it will take some runtime. I'm
> wondering what is the practice that take care of the assertion code in
> python.

Read the manual:

http://docs.python.org/reference/simple_stmts.html#the-assert-statement

"In the current implementation, the built-in variable __debug__ is
True under normal circumstances, False when optimization is requested
(command line option -O). The current code generator emits no code for
an assert statement when optimization is requested at compile time."

HTH,
~Simon
--
http://mail.python.org/mailman/listinfo/python-list


aahz at pythoncraft

Nov 26, 2009, 9:43 AM

Post #3 of 9 (247 views)
Permalink
Re: Anything equivalent to cassert in C++? [In reply to]

In article <mailman.580.1258492799.2873.python-list [at] python>,
Peng Yu <pengyu.ut [at] gmail> wrote:
>
>There are some assertion code (testing if a condition is false, if it
>is false, raise an Error object) in my python, which is useful when I
>test my package. But such case would never occur when in the produce
>code. If I keep them in if statement, it will take some runtime. I'm
>wondering what is the practice that take care of the assertion code in
>python.

You should be very careful not to put unit tests in asserts because that
prevents you from testing your application under optimization.
--
Aahz (aahz [at] pythoncraft) <*> http://www.pythoncraft.com/

The best way to get information on Usenet is not to ask a question, but
to post the wrong information.
--
http://mail.python.org/mailman/listinfo/python-list


jeanmichel at sequans

Nov 26, 2009, 10:08 AM

Post #4 of 9 (247 views)
Permalink
Re: Anything equivalent to cassert in C++? [In reply to]

Peng Yu wrote:
> There are some assertion code (testing if a condition is false, if it
> is false, raise an Error object) in my python, which is useful when I
> test my package. But such case would never occur when in the produce
> code. If I keep them in if statement, it will take some runtime. I'm
> wondering what is the practice that take care of the assertion code in
> python.
>
Would never occur ? You can't use 'would' and 'never' in the same
sentence. That is the kind of concect that make (to me) asserts diffcult
to use.
I personally never use asserts in python, cause I will end up handling
with asserts what I should in fact handle properly in what you call the
production code.

Anyway that does not answer your question I'm sorry to be off topic,
'tis just a thought. Simon gave you the proper answer.

Jean-Michel
--
http://mail.python.org/mailman/listinfo/python-list


lie.1296 at gmail

Nov 26, 2009, 10:18 AM

Post #5 of 9 (247 views)
Permalink
Re: Anything equivalent to cassert in C++? [In reply to]

Jean-Michel Pichavant wrote:
> Peng Yu wrote:
>> There are some assertion code (testing if a condition is false, if it
>> is false, raise an Error object) in my python, which is useful when I
>> test my package. But such case would never occur when in the produce
>> code. If I keep them in if statement, it will take some runtime. I'm
>> wondering what is the practice that take care of the assertion code in
>> python.
>>
> Would never occur ? You can't use 'would' and 'never' in the same
> sentence. That is the kind of concect that make (to me) asserts diffcult
> to use.
> I personally never use asserts in python, cause I will end up handling
> with asserts what I should in fact handle properly in what you call the
> production code.
>
> Anyway that does not answer your question I'm sorry to be off topic,
> 'tis just a thought. Simon gave you the proper answer.
>
> Jean-Michel

Actually, if an assert statement is never raised in production code
irrespective of the condition (even in case of malicious inputs), it is
the correct use of assert statement. The difficulty is in proving that
it is impossible to raise the assert. In most cases it is impossible to
prove and we just turn off assert when we're quite confident that it is
bug-free enough.
--
http://mail.python.org/mailman/listinfo/python-list


invalid at invalid

Nov 26, 2009, 10:58 AM

Post #6 of 9 (249 views)
Permalink
Re: Anything equivalent to cassert in C++? [In reply to]

On 2009-11-26, Jean-Michel Pichavant <jeanmichel [at] sequans> wrote:
> Peng Yu wrote:
>> There are some assertion code (testing if a condition is false, if it
>> is false, raise an Error object) in my python, which is useful when I
>> test my package. But such case would never occur when in the produce
>> code. If I keep them in if statement, it will take some runtime. I'm
>> wondering what is the practice that take care of the assertion code in
>> python.
>
> Would never occur? You can't use 'would' and 'never' in the
>same sentence.

Why not? "Would never" is a very common English phrase, and
the OP used it correctly.

--
http://mail.python.org/mailman/listinfo/python-list


python at mrabarnett

Nov 26, 2009, 12:49 PM

Post #7 of 9 (246 views)
Permalink
Re: Anything equivalent to cassert in C++? [In reply to]

Grant Edwards wrote:
> On 2009-11-26, Jean-Michel Pichavant <jeanmichel [at] sequans> wrote:
>> Peng Yu wrote:
>>> There are some assertion code (testing if a condition is false, if it
>>> is false, raise an Error object) in my python, which is useful when I
>>> test my package. But such case would never occur when in the produce
>>> code. If I keep them in if statement, it will take some runtime. I'm
>>> wondering what is the practice that take care of the assertion code in
>>> python.
>> Would never occur? You can't use 'would' and 'never' in the
>> same sentence.
>
> Why not? "Would never" is a very common English phrase, and
> the OP used it correctly.
>
As in "I would never tell a francophone how to speak French". :-)
--
http://mail.python.org/mailman/listinfo/python-list


bruno.42.desthuilliers at websiteburo

Nov 27, 2009, 1:41 AM

Post #8 of 9 (240 views)
Permalink
Re: Anything equivalent to cassert in C++? [In reply to]

Jean-Michel Pichavant a écrit :
(snip)
> I personally never use asserts in python, cause I will end up handling
> with asserts what I should in fact handle properly in what you call the
> production code.

Not the same concern. Assertions are here for 1/ documention of expected
state / value / pre|post conditions etc, and 2/ automatic verification
of these expectations *during development* - so you can immediatly spot
and fix failed expectations. IOW, it's about the "this should not, no
way, never possibly happen" cases, not the "this may eventually happen"
ones - which indeed require "proper" handling at the level you expect them.


--
http://mail.python.org/mailman/listinfo/python-list


tartley at tartley

Nov 27, 2009, 6:15 AM

Post #9 of 9 (235 views)
Permalink
Re: Anything equivalent to cassert in C++? [In reply to]

On Nov 26, 6:08 pm, Jean-Michel Pichavant <jeanmic...@sequans.com>
wrote:
> Peng Yu wrote:
> > There are some assertion code (testing if a condition is false, if it
> > is false, raise an Error object) in my python, which is useful when I
> > test my package.  But such case would never occur when in the produce
> > code. If I keep them in if statement, it will take some runtime. I'm
> > wondering what is the practice that take care of the assertion code in
> > python.
>
> Would never occur ? You can't use 'would' and 'never' in the same
> sentence. That is the kind of concect that make (to me) asserts diffcult
> to use.
> I personally never use asserts in python, cause I will end up handling
> with asserts what I should in fact handle properly in what you call the
> production code.
>
> Anyway that does not answer your question I'm sorry to be off topic,
> 'tis just a thought. Simon gave you the proper answer.
>
> Jean-Michel


I can see this issue has several facets to it, so I don't intend to
flagratly contradict your experience Jean-Michel, but I think that in
at least some circumstances asserts used in this way can be valuable.

One of the interesting points to come out of the recent flurry of
empirical studies of software engineering is that quality of code as
measured by defect density shows a definite negative correllation with
use of asserts in production code. (Although the significance of this
is somewhat lessened by the fact that both use of asserts and low
defect density are correlated with the level of developer experience.)

Original Microsoft paper here:
http://research.microsoft.com/apps/pubs/default.aspx?id=70290
Summary article discussing that paper and others:
http://research.microsoft.com/en-us/news/features/nagappan-100609.aspx


Personally, I'm curious as to whether the benefit from asserts used in
this way is in helping the developer to clarify in their own mind
exactly what their expectations and invariants are.
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.