Does eval has the same features as Perl's?

Paddy paddy3118 at netscape.net
Sat Jan 20 17:11:10 EST 2007


Jm lists wrote:

> Hello members,
>
> I want to know does the "eval" in python have the same features as in
> Perl (capture errors)?
>
> For example,in perl I can wrote:
>
> $re = eval { 1 / 0 };
>
> Though 1/0 is a fatal error but since it's in "eval" block so the perl
> interpreter doesn't get exit.
>
> Thanks again.
Hi Jim,
If your new to Python and coming from Perl then your question above
needs knowledge of Python Exceptions, and the try statement.

if you had variables x and y and wanted to compute x/y but if y was
zero print some message and continue, then you would most likely do
something like:

x = 1
y = 0
try:
  z = x / y
except ZeroDivisionError:
  print "This leads to a divide by zero error!"


There is more info in the tutorial chapter 8.

- Paddy.




More information about the Python-list mailing list