Does eval has the same features as Perl's?

J

Jm lists

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.
 
S

Steven D'Aprano

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.

How hard would it be to actually try it?
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero

It took about three seconds to actually test it.

eval has many security risks -- as a rule, you should never pass strings
you've got from the user to eval, unless you want the user to p0wn your
computer. It is harder than you might think to make eval safe -- you
should seriously consider it an advanced tool, not a basic tool. As a
newbie, you should work under the following rule:

"If I think I need to use eval, I'm probably wrong."

I've been using Python for seven or eight years, and I don't think I've
ever used eval in serious code.

Now, suppose you find yourself wanting to use eval. You've considered the
above rule carefully, and decided that it doesn't apply in this case.
You've been careful to use it only on safe strings, not arbitrary strings
from users. How do you use eval so it captures errors?

try:
eval("1/0")
except ZeroDivisionError: # capture only one error
pass


or something like this:

try:
eval("something or other goes here")
except Exception: # capture any error
pass
 
P

Paddy

Jm said:
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.
 
J

J. Clifford Dyer

The python editor won't "get exit." It will raise an exception. With
or without an eval, you can catch the exception.

try:
x = 1/0
except ZeroDivisionError:
x = "infinity"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top