[Python-ideas] Arguments to exceptions

Terry Reedy tjreedy at udel.edu
Wed Jul 5 00:54:05 EDT 2017


On 7/5/2017 12:21 AM, Terry Reedy wrote:
> On 7/4/2017 6:31 PM, Greg Ewing wrote:
>> Terry Reedy wrote:
>>> Attaching a *constant* string is very fast, to the consternation of 
>>> people who would like the index reported.
> 
> Actually, the constant string should be attached to the class, so there 
> is no time needed.

I should say, a default string, or parameter default value.  I just 
checked and Python gives the numerator type the ZeroDivisionError 
message and the sequence type in the IndexError message, so scratch that 
idea.

>> Seems to me that storing the index as an attribute would help
>> with this. It shouldn't be much slower than storing a constant
>> string,
> 
> Given that the offending int is available as a Python int, then storing 
> a reference should be quick, though slower than 0  (see above ;-).

0 is wrong.  Just a reference storage in both cases.

>> and formatting the message would be deferred until
>> it's needed, if at all.
> 
> I agree that this would be the way to do it.  I will let an advocate of 
> this enhancement lookup the rejected issue (there may be more than one) 
> proposing to make the bad index available and see if this is the actual 
> proposal rejected and if so, why (better than I may remember).

> It occurs to me that if the exception object has no reference to any 
> python object, then all would be identical and only one cached instance 
> should be needed.  I checked and neither IndexError nor 
> ZeroDivisionError do this.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list