4. How does destruction work?#

4.1. example#

We create a list of real numbers with the name listr

We are destroying the concept of name list

Note that reusing the same Python variable will have the same effect as DETRUIRE.

4.2. In detail#

The objects created by code_aster (a mesh, a result) are Python objects.

DETRUIRE removes references to the object in the command set. Python actually removes this object when there is no longer a reference to this object.

Let’s take the example:

mesh = LIRE_MAILLAGE ()

alias = mesh

lst = [mesh]

We created a mesh object, mesh is a variable that references (or « that gives access to », or « that points to ») this object. alias is a variable that references the same object. lst is a list, created with the [] operator, that contains a single element, and this element also references the same mesh object.

If we do:

DETRUIRE (NOM =mesh)

All direct references to the mesh object are removed from the command set.

So the mesh and alias variables are removed.

Moreover, during the execution, we see:

Remove reference: “mesh”

Deleting the reference: “alias”

On the other hand, the reference via the list is not seen (you would have to go through all types of objects!) and therefore, in accordance with the way Python works, the mesh object is not actually destroyed since there is still a reference to this object.

If we delete the list:

Del LST

or the list item:

Del LST [0]

Then there is no longer any reference to the object which is then effectively destroyed. We can see:

Deleting 00000004 <MAILLAGE_SDASTER> mesh

Note that:

DETRUIRE (NOM =lst [0])

would have had exactly the same behavior (removing mesh and aliases but not lst [0]) because we pass the object to DETRUIRE and not the lst list or the index 0.

We would also have the same behavior with a dictionary containing a mesh object or any object referencing the mesh.

In fact, only the container (the list object, dictionary, or other object) knows how to delete one of its elements.

4.3. Dependency between objects#

Data structures (field at node-numbering, field by element-model, etc.) rely on each other. DETRUIRE a data structure that is used by another therefore has no immediate effect. On the other hand, when all references to a data structure are removed, the data structure is then effectively removed.