Debugging unintentional assignments using id
In this section, we will be discussing what happens when we make unintentional references or assignments to an object while defining an attribute, a method, or a function, and how to resolve such incorrect assignments using the built-in id
function. When a reference is made unintentionally, the memory address of the object is shared between the actual object and the reference object. In this example, we will be making use of id
to debug the Python objects created in the preceding section and identify duplicate assignments or references of an object that might have been created unintentionally while developing an application. Here is how it works:
- To begin with, let’s create a dictionary item,
class_id_count
, to capture the number of occurrences of the memory address of each class:class_id_count = {}
- In the next step, we will be creating the following four lists:
duplicates = [] ids = [] classes = [] classnames = []