How to make edits worth while debugging?

2

When I debug a code in Pycharm (Shift + F9) and make some changes, it is not recognized during the same debugging session. I am required to restart the debug again for the changes to be acknowledged.

Is there any way for Pycharm to recognize the changes DURING debug?

    
asked by anonymous 01.08.2018 / 02:57

1 answer

2

It would be nice if you put an example of what variables you are trying to change. But anyway, if you're trying to change local variables in a debugging function, this is not possible because of how Python is implemented:

The internal language mechanisms that allow debugging and others always update local variables in a "read only" way: the debugger or any called function can see local variables accessing the f_locals attribute of the execution frame of the function being inspected.

However, although up to version 3.7, f_locals contain a normal dictionary, which allows changing values, these non values are copied back to the local variables of the function when it really is performed. Until today, this was more or less an "implementation detail" of CPython, indicated in some parts of the documentation - but is being formalized as part of the language specification at PEP 558 .

In summary: neither the debugger of pycharm, nor any other debugger, nor any functionality that does not violate the specifications of the language to do "magic" in values of a function that calls others (explicitly, or implicitly, as it is the case in debugger operation), you can change the value of a local variable.

However , if you need this to streamline your workflow, there is a workaround : objects that are associated with names in local variables can not be < in> exchanged , but can be changed . That is, if your content of your variable is a number or a string, there is nothing that can be done. But if its content is a list, dictionary, or other mutable object, that object can be changed from the context of the debuggers.

So let's assume your function has a id variable you'd like to be able to change interactively from within the debugger - you can (maybe temporarily) change your code so that the values you're interested in are within a list :

Instead of:

def minha_func(id):
   ...
   consulta_ao_banco(id)
   ...

You can do:

def minha_func(id):
   id = [id]
   ...
   # neste trecho do código, o conteúdo da lista em 'id' pode 
   # ser alterado de dentro do debugger
   ...
   consulta_ao_banco(id[0])
   ...
    
02.08.2018 / 17:03