classification
Title: exception item from mapped function
Type: enhancement Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, doerwalter, georg.brandl
Priority: normal Keywords:

Created on 2001-08-02 11:37 by anonymous, last changed 2007-08-23 19:12 by georg.brandl. This issue is now closed.

Messages (7)
msg53208 - (view) Author: Nobody/Anonymous (nobody) Date: 2001-08-02 11:37
The purpose of this enhancement would be to get the 
item in a list that caused an exception when applying 
a mapped function.

For instance, suppose that I want to detect if there 
is an item in a list that is not a number. For this 
illustration I try to map float on the list and then 
catch the exception. 

try:
   map(float, mylist)
except ValueError:
   print "The item %s in mylist is not a number." % 
(my_item)

The problem is that I do not know which item in the 
list caused the exception. How do I get my_item?


This information could probably be useful in a number 
of different contexts.

Thank you. 
msg53209 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2003-05-13 00:59
Logged In: YES 
user_id=357491

Well, if you were doing this with a list comprehension it isn't hard::

    try:
        float_list = [float(x) for x in mylist]
    except ValueError:
        print "The item %s in mylist is not a number." % x

Since list comprehensions are practically just a 'for' loop in a much tighter 
way the variables used stick around just as if you had used a 'for' loop.

The other issue is the way errors propogate in C.  If map were to return its 
own error specifying which item caused a problem it would have to overwrite 
the exception that was raised in the first place and overwriting exceptions on 
the whole is bad.

If you really are in love with map you can always define your own wrapper 
around float to raise the exception you want::

    def myfloat(num):
        try:
            return float(num)
        except ValueError:
            raise ValueError("The item %s in mylist is not a number." % num)

I personally feel this RFE should be rejected.  Anyone else agree?
msg53210 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2003-05-13 07:01
Logged In: YES 
user_id=89016

I we had exception chaining (i.e. each exception can
reference another exception, that is the cause for this
one), this would just be a special case. A traceback could
then look like this:

>>> map(float, [0, 1, None, 3])
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: in entry None at position 2
Caused by:
TypeError: float() argument must be a string or a number
msg53211 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2003-05-13 07:04
Logged In: YES 
user_id=357491

True.  I remember this being discussed at one point and people thinking it 
was a good idea but it never got implemented.
msg53212 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2003-05-13 07:21
Logged In: YES 
user_id=89016

I'm still planning to give this a try someday, but maybe
this should be part of the big "new style exceptions PEP"?
msg53213 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2003-05-13 19:31
Logged In: YES 
user_id=357491

Considering it will probably require changing the binary layout of exceptions 
I would have to agree that a PEP is in order.
msg55185 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-08-23 19:12
Closing, see PEP 3134.
History
Date User Action Args
2007-08-23 19:12:26georg.brandlsetstatus: open -> closed
nosy: + georg.brandl
messages: + msg55185
title: exception item from mapped function -> exception item from mapped function
2001-08-02 11:37:34anonymouscreate