Why doesn't Python (error msg) tell me WHAT the actual (arg) values are ?

dn PythonList at DancesWithMice.info
Thu Feb 23 22:12:10 EST 2023


On 23/02/2023 09.05, Hen Hanna wrote:
> 
>            >  py   bug.py
>         Traceback (most recent call last):
>           File "C:\Usenet\bug.py", line 5, in <module>
>                       print( a + 12 )
>            TypeError: can only concatenate str (not "int") to str
> 
> 
> Why doesn't  Python (error msg) do the obvious thing and tell me
>              WHAT   the actual   (offending,  arg)  values are ?
> 
> In many cases, it'd help to know what string the var  A  had  ,   when the error occurred.
>               ------------  i wouldn't have to put      print(a)     just above,  to see.

In some ways, providing this information seems appropriate. Curiously, 
this does not even occur during an assert exception - despite the 
value/relationship being the whole point of using the command!

     x = 1
     assert x == 2

AssertionError (and that's it)

Then again, remember that exceptions can be 'caught'. So, such data 
would need to be added to the exception-instance. This could become 
quite costly.



What are the appropriate tools for the job?


Don't add an extra print(), use a debugger.

Not only does this allow you to breakpoint critical points in the code, 
but identifiers can be watch-ed and changes noted. The other handy 
feature is being able to correct the current erroneous value of the 
identifier and continue execution.

For us, memory-challenged coders, there is no need to remember to remove 
the print() again, afterwards.


The TypeError indicates a problem between the programmer's ears. What 
was thought to be a string or an integer was the opposite. This seems to 
be playing fast-and-loose with Python's dynamic-typing. To quote: "we're 
all adults here". Thus, I wouldn't recommend 're-cycling' an identifier 
to represent two different (types of) data-point in the same code - but 
there's nothing to stop you/anyone!

The other possibility is that it was an accident. Sounds more like 
something I would do, but... In this case, the tool which is your/my 
friend is typing. The IDE should catch most of the situations where an 
int would be used as an str, or v-v. Remember though, Python's typing is 
(a) not part of the language, and (b) probably won't help at run-time.


PS are you aware that there is a Python-Tutor list for the use of people 
learning Python?
-- 
Regards,
=dn


More information about the Python-list mailing list