Re: [Tutor] about assert

2006-09-13 Thread Marc Poulin

--- [EMAIL PROTECTED] wrote:

> >>Python manual has a very brief introduction 
> >>of "assert" statements. It is very difficult 
> 
> First you probably don't need to use asserts 
> very often, they are useful if you are building 
> production strength code but for most users 
> of Python the exception mechanism is good enough.
> 

This link makes some good points about how and when
to use assertions:
http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] about assert

2006-09-13 Thread alan . gauld
>>Python manual has a very brief introduction 
>>of "assert" statements. It is very difficult 

First you probably don't need to use asserts 
very often, they are useful if you are building 
production strength code but for most users 
of Python the exception mechanism is good enough.

Basically we use asserts to check pre and post 
conditions as well as\ invariants in functions.
Anything other than that should be done using 
explicit if/else or exceptions. The reason for 
that is that asserts only operate when the code 
is in debug mode... so they should not be 
used for normal run time error detection.

Typical things to check are that input values 
are within expected ranges and of desired types.
Also that the final retrurn value of a function 
is within expected limits.
Once we finish testing the overhead of those 
checks might not be required in the final 
version if we are sure they will never 
deviate from the test results.

The syntax can be demonstrated quite easily 
and Luke has done that, the intent is a little 
less obvious and IMHO really needs a larger 
example to make the value obvious. Since I'm 
at a cyber-cafe net terminal I dont feel like 
trying that just now! :-)

HTH,

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] about assert

2006-09-13 Thread Marc Poulin

--- "linda.s" <[EMAIL PROTECTED]> wrote:

> Python manual has a very brief introduction of
> "assert" statements. It
> is very difficult for me to understand it.


Every program has some fundamental assumptions that
must remain true in order for the program to continue
giving correct results.

The assert statement is used to verify those
assumptions. (The optional 2nd parameter can be used
to give additional information about what went wrong.)

For example, in my world no one is allowed to have a
negative age. A negative age means my program is
hopelessly confused and should halt immediately.

>>> myAge=42
>>> assert myAge>=0  ## this is OK

>>> myAge= -1## logically impossible
>>> assert myAge >= 0
Traceback (most recent call last):
  File "", line 1, in ?
AssertionError

## here I print the condition that failed
>>> assert myAge >=0, 'myAge >= 0'
Traceback (most recent call last):
  File "", line 1, in ?
AssertionError: myAge >= 0


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] about assert

2006-09-13 Thread Luke Paireepinart
linda.s wrote:
> Python manual has a very brief introduction of "assert" statements. It
> is very difficult for me to understand it. Can anyone give me an
> example?
>   
#example of assert.py
def test(a):
if 25 / 4 == a:
return True
else:
return False

def assert_it(a):
try:
assert a
print "Success!"
except AssertionError:
print "something went wrong!"
f = test(4)
assert_it(f)
f = test(6)
assert_it(f)

#--- end code

It seems to me that you don't really need to know what assert is,
because the value of __debug__ will probably be true,
and so you might as well write if not a: raise AssertionError
instead, except I guess assert is shorter.
But then if you use assert assuming __debug__ will be true,
then if it's not bad things will happen.
I'm sure someone can give you a better explanation of this,
but I have to get to class.
HTH,
-Luke
> Thanks,
> Linda
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor