Deleting an object

Hello Everyone !
I actually have these 2 lines that look for the class and instantiate them :

covergroup_names = [x for x in dir(coverage_points) if isclass(getattr(coverage_points, x))]
covergroup_insts = [globals()[groupname].inst(uut) for groupname in covergroup_names]

So at that stage I’m instatiating all the objects

But the thing is, as you can see, I’m always calling the method inst() which verifies if the class is instantiated or not

@vsc.covergroup
class internal_coverage(object):
    _inst=None

    def __init__(self,uut):     
            Entity_Name= samp.GetEntityName(uut)
            print("ENTITY NAME :  ", Entity_Name)
            setattr(self,Entity_Name,ccc.coverpoint(uut,lambda : getattr(uut.adder,"c"), bins={  
                str(63-i):vsc.wildcard_bin("0b" + "x" * (63-i) + "1" + "x" * (i)) for i in range(64)
            }))


    @classmethod
    def inst(cls,uut):
        if cls._inst is not None:
            cls._inst = cls(uut)
        if cls._inst is None:
            cls._inst = cls(uut)
        return cls._inst

But the problem is with this, method, if I would like to instantiate this class again but with a different parameter (in this case uut usually changes), it won’t take into account this modification because it’s already instantiated with the previous ‘uut’
So I think an idea that came up, is to delete the object one it’s finished.
So how can I do that ? Or at least maybe, how can we give a new parameter to an object already instatiated

a) You can create reset() method which sets the object to an initial state. It could be the same as your current __init__() is and you can call it from __init__().

    def __init__(self, uut):     
        self.reset(uut)

    def reset(self, uut):     
        ...

b) Python has garbage collection so when there is no reference to an object, it gets deleted automatically.

a = object()
a = None  # After this command the original object instance is deleted.
del a     # This has the same effect on the object instance when used instead.

del in addition to removing the object reference also removes the variable.


Note that in your inst() method you are always creating a new instance regardless what cls._inst contains.

1 Like

What is the purpose of that internal_coverage._inst class attribute?

It seems to be the cause of your difficulty, but I don’t know why it exists. It only gets used in the inst method, but it does nothing:

    @classmethod
    def inst(cls,uut):
        if cls._inst is not None:
            cls._inst = cls(uut)
        if cls._inst is None:
            cls._inst = cls(uut)
        return cls._inst

So **it doesn’t matter whether cls._inst is None or not, both conditions run the same code. So you can re-write that method as:

    @classmethod
    def inst(cls,uut):
        return cls(uut)

I think that maybe you are trying to create a Singleton? If so, why? Unless you have a really good reason, forget about singletons, they are usually a bad idea.

In your case, you don’t want a singleton, because you need different instances created from different uut values. So no singleton.

Instead of having your class store the your instances share the same data stored on the class, you should give each instance their own state.

Then each instance can be instantiated from a different uut, and you can have as many instances as you want.

1 Like

This is a great technique, Václav. I can use this now. Thank you!

I am glad that you find it useful. Some additional notes:

  • Technically you can call __init__() directly but this could be confusing so I think it is better to let __init__() for its original purposes: automatic call during object creation and explicit calls in situations like super().__init__() in a sub-class’ __init__().
  • Note that someone can expect that you create all the object’s attributes inside __init__(). To avoid this confusion I would add a comment inside __init__() saying that reset() creates the object’s attributes.
1 Like