Is it wise to store items by memory (pyqt)?

python

#1

Hi all, in my tool, I am trying to store the list of QMenus and QActions as a dictionary, so that I will be using it at a later point.

Instead of storing them by their names, I am actually storing them by their memory names (not sure if there is a term for this) and from time to time, the items (values) in the dictionary are wrong.

This is an example of my captured data:

{0: {<qt5.QtGui.QMenu object at 0x7ff6e5f238c0>: [<PySide2.QtWidgets.QAction object at 0x7ff6e5f25b48>,
                                                                     <PySide2.QtWidgets.QAction object at 0x7ff6e60e27a0>,
                                                                     <PySide2.QtWidgets.QAction object at 0x7ff6e60e2290>],
     <qt5.QtGui.QMenu object at 0x7ff6e60e21b8>: [<PySide2.QtWidgets.QAction object at 0x7ff6e5f25b48>,
                                                                     <PySide2.QtWidgets.QAction object at 0x7ff6e60e27a0>,
                                                                     <PySide2.QtWidgets.QAction object at 0x7ff6e60e2290>]}}

As you can see, I have 2 different keys but for some reasons, its items within are the same in which it should not be. (In my test case, 1 contains 2 items, the other contains 3 items.)

Even so, my main question here is - is it wise to store items by memory (do let me know if there is an actual term for it)?
(Pardon me for my coding terms as I doing self-learning)


#2

There is no problem with using an object as a key in a dictionary. As long as that object is hashable the dictionary shouldn’t complain.


#3

Thanks for getting back to me on this, R.White!

I had thought it could be due to me making use of the object(s) as I am getting varying results in my dictionary. Guess I will need to take a deeper look into my code.

Thanks again!


#4

The only way that it might be a problem, is that if Qt remakes new instances of those objects from some underlying pointer that only it sees. In that case because you’re actually get a new instance of the QMenu or QAction, they wouldn’t actually line up anymore.

But I don’t know enough about the surrounding code, nor how Qt handles that kind of thing.