Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail From: ram@zedat.fu-berlin.de (Stefan Ram) Newsgroups: comp.lang.python Subject: Re: how to discover what values produced an exception? Date: 3 May 2024 14:26:58 GMT Organization: Stefan Ram Lines: 75 Expires: 1 Feb 2025 11:59:58 GMT Message-ID: References: <8734qz9ey0.fsf@tudado.org> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de 09xumdQQZsY92pxfm79zxQBprX/BKxlDyyL1ahb7RwBSB4 Cancel-Lock: sha1:cYhD6NaHQJZZl5vE7cXP801Nfc0= sha256:FU2WceNYa9o9olmj3bTwdnPmIcCsBQEk7afIoe9UlZo= X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed. X-No-Archive: Yes Archive: no X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access. X-No-Html: yes Content-Language: en-US Bytes: 3536 Johanne Fairchild wrote or quoted: >How to discover what values produced an exception? An exception isn't always tied to a value. I could, like, write: if 3 > 2: "".call() And I'd get an AttributeError exception. Can I now say the values 3 and 2 caused this exception? Or was it the empty string? Or the lack of a call method? Let me try your example here: In the console: |>>> ( 0, 0 )< 4 |TypeError: '<' not supported between instances of 'tuple' and 'int' In this case, the values aren't important, one could even say they're a distraction. What matters are the types of the values, and those were given. However, the values are visible in the console as they were typed in. In a script: |Traceback (most recent call last): | File "Main.py", line 1, in | ( 0, 0 )< 4 |TypeError: '<' not supported between instances of 'tuple' and 'int' . Now I can see the offending line, which in this case even spills the beans on the values. Sometimes you really need those values. If they're values of global names, you can kind of get them in the IDLE shell up to a point. So I write this script in the IDLE editor and then run it: a = 4 ( 0, 0 )< a . Now I get this in the IDLE console: |Traceback (most recent call last): | File "Main.py", line 2, in | ( 0, 0 )< a |TypeError: '<' not supported between instances of 'tuple' and 'int' . Then I can type a into the console, hit Return, and I see the value 4. This script here has local variables, and that trick ain't gonna fly no more: def main(): a = 4 ( 0, 0 )< a main() Now you got to add a debug print statement and run the script again. def main(): a = 4 print( a ) ( 0, 0 )< a main() In a bigger script there's all kinds of values and variables, and sometimes the Python implementation can't know which ones are critical for an exception, so the programmer's got to step in and write those debug print statements. (You could also use the IDLE debugger to see local variables, but I prefer debug print statements.)