How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

Cameron Simpson cs at cskk.id.au
Tue Jan 26 19:57:35 EST 2021


On 26Jan2021 14:00, C W <tmrsg11 at gmail.com> wrote:
>I'm a long time Matlab and R user working on data science. How do you
>troubleshooting/debugging in Python?
>
>I ran into this impossible situation to debug:
>class person:
>def __init__(self, id, created_at, name, attend_date, distance):
>"""Create a new `person`.
>"""

Your mailer has removed all the indenting. See if it has a mode for 
code.  Often just making sure all the code is itself indented will do, 
eg:

    some indented
        code here

>I got an error message saying id was 'str', but expecting 'int'.

There should have been a complete traceback with that error, showing 
which line triggered the exception and how it was called.

Here, we don't know from your description what happened or where.

>If this error was in R, I would have tried:
>> self._id = 123

Well, provided "self" already existed, that would work in Python but 
tell you almost nothing, because any variable may reference a value of 
any type. (Python variables are not typed, but the values _are_.)

>But, I can't do that here! What do I do? Do I HAVE TO instantiate an 
>object
>first?

Well, "self" is the instantiaited object. The __init__ function does 
more setup (completing what you might call "normal" instantiation).

>It's not  convenient if I have 10 of these objects around. I need to
>instantiate 10 objects.

Your environment isn't clear to me.

>I know hardcore computer scientists would tell me about Python debugger. R
>also has one, but nobody ever uses it. I don't find them user-friendly!

Me either. Like grant, i fall into the "look at the stack trace and 
think a bit, then put in print() calls or similar to show me whether 
things are what I expect them to be when the code runs".

I actually have a Computer Science degree, but I think there are 
definition those who like debuggers and those who tend not to. I'm in 
the latter camp. Preferences like that affact how you _choose_ to debug.

Usually I have the code in a file in one window and a command line in 
another, and go:

    python my_code_file.py ...

to test when debugging.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list