Does eval has the same features as Perl's?

Discussion in 'Python' started by Jm lists, Jan 20, 2007.

  1. Jm lists

    Jm lists Guest

    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.
     
    Jm lists, Jan 20, 2007
    #1
    1. Advertisements

  2. 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
     
    Steven D'Aprano, Jan 20, 2007
    #2
    1. Advertisements

  3. Jm lists

    Jm lists Guest

    Thank you.I'm just learning Python and want to make something clear to me.:)
     
    Jm lists, Jan 20, 2007
    #3
  4. Jm lists

    Paddy Guest

    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.
     
    Paddy, Jan 20, 2007
    #4
  5. 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"
     
    J. Clifford Dyer, Jan 22, 2007
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.