On Wed, May 11 2016, Roberto Polli wrote:
> Il 11 maggio 2016 16:31, Pietro Battiston <m...@pietrobattiston.it> ha 
> scritto:
> > ... condensare ...
> >
> > if not civico.isdigit():
> >     raise ValueError("Il numero civico deve essere numerico")
> >
> > in
> >
> > assert(civico.isdigit()) "Il numero civico deve essere numerico"
> >
> Uso assert se il controllo:
> 
>   - non deve influire sul flusso del programma;
>   - rappresenta condizioni inaspettate e quindi un bug del programma;
>   - come placeholder nelle fasi preliminari - chiarito meglio il
> flusso li sostituisco con eccezioni;
> 
> eg.
> 
> ```
> ret = factorial(n)
> assert ret, "Factorial can't be negative. BUG in the program!"
> print(ret)
> ```

try:
    # your code
except AssertionError:
    raise AssertionError("BUG in the assertion")
else:
    if x < 0:
        raise AssertionError("BUG in the assertion")
_______________________________________________
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python

Rispondere a