<div dir="auto">If a method, why not a property?</div><div class="gmail_extra"><br><div class="gmail_quote">On Jul 4, 2017 2:41 PM, "Terry Reedy" <<a href="mailto:tjreedy@udel.edu">tjreedy@udel.edu</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 7/4/2017 3:32 PM, David Mertz wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I don't see the usefulness rich exception data as at all as limited as this. Here's some toy code that shows a use:<br>
<br>
<br>
----<br>
<br>
# For some reason, imports might not be ready immediately<br>
# Maybe flaky network drive, maybe need to install modules, etc<br>
# The overall program can do things too make them available<br>
lazy_import("foo", "bar", "baz", "blat")<br>
<br>
while True:<br>
     try:<br>
         x = foo(1) * bar(2) + baz(3)**blat(4)<br>
         break<br>
     except NameError as err:<br>
         lazy_import(<a href="http://err.name" rel="noreferrer" target="_blank">err.name</a> <<a href="http://err.name" rel="noreferrer" target="_blank">http://err.name</a>>)<br>
         sleep(1)<br>
</blockquote>
<br>
Alternate proposal: give the NameError class a .name instance method that extracts the name from the message.  This should not increase the time to create an instance.  You would then write '<a href="http://err.name" rel="noreferrer" target="_blank">err.name</a>()' instead of '<a href="http://err.name" rel="noreferrer" target="_blank">err.name</a>'. For 3.6<br>
<br>
def name(self):<br>
    msg = self.args[0]<br>
    return msg[6:msg.rindex("'")]<br>
<br>
# current test<br>
<br>
try: xyz<br>
except NameError as e:<br>
    print(name(e) == 'xyz')<br>
<br>
# Exceptions unittest to ensure that the method<br>
# stays synchronized with future versions of instances<br>
<br>
def test_nameerror_name(self):<br>
    try:<br>
        xyz<br>
    except NameError as e:<br>
        self.assertEqual(<a href="http://e.name" rel="noreferrer" target="_blank">e.name</a>(), 'xyz')<br>
<br>
Generalize to other exceptions.<br>
<br>
Further only-partially baked idea: Since exceptions are (in cpython) coded in C, I wonder if C data could be inexpensively attached to the instance to be retrieved and converted to python objects by methods when needed.<br>
<br>
-- <br>
Terry Jan Reedy<br>
<br>
______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofco<wbr>nduct/</a><br>
</blockquote></div></div>