How to copy the default arguments of a function and initialize them as variables for debugging purposes in Python?

In my workflow I use Python interactively with Jupyter notebooks. Often I need to check some code inside a function with default arguments, e.g.

def f (a = 0, b = 1, c = 2, d = 3):
   return a, b, c, d

In order to do this, I tipically run in new cells parts of the code inside the function and verify that everything is calculated correctly. However, because the default arguments are not stored in the memory, I need before to copy and paste them in another cell and initialize them. But I can not just do:

a = 0, b = 1, c = 2, d = 3

As I get:

SyntaxError: cannot assign to literal.

But I need to edit the different variables removing the commas and adding a change of line, as in:

a = 0
b = 1
c = 2
d = 3

This is quite tedious when I have a lot of variables.

Is there a Pythonic alternative to declare and initialize these variables in the same line without editing them?

2 answers

  • answered 2022-05-04 14:53 Wayne

    Not really following your issue here. Sadly you cannot paste code blocks into comments, and so I'm using this.
    The reason I don't follow, is that you already have a way to define the default variables in the function assignment. You even show it. You just need to call your function to then debug it because you say you don't want to edit them? Function in its own cell would look like this:

    def f (a = 0, b = 1, c = 2, d = 3):
       return a, b, c, d
    f()
    

    Then if you did want to edit them, you can define one or all of them on the same line by changing the commas to semi-colons. Function in its own cell would look like this:

    def f (a = 0, b = 1, c = 2, d = 3):
        a = 8; b = 9; c = 7; d = 1
        return a, b, c, d
    f()
    

    The first code block returns the defaults. The second returns the new settings.

    This probably isn't what you want; however, maybe it will help you refine your question to be clear.

  • answered 2022-05-04 15:14 rioV8

    In the debug console when you hit a breakpoint in f type

    a,b,c,d = 0,1,2,3
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum