views:

121

answers:

1

Hi everyone,

I'm currently coding a project in python where I need a sort of cache of generic objects, I have settled on using WeakValueDictionaries for this. These generic objects are often referenced by many other non-generic objects. My main problem though is that I can't seem to wrap my head around a way of making these WeakValueDictionaries available to many different portions of the program. I would prefer not to use "global" variables if possible.

Best regards

FrederikNS

+1  A: 

Maybe I'm not understanding your question, but making a dictionary of weakly referenced values available to your code isn't really any different from making a dictionary of anything else available to your code. I would store a reference to the WeakValueDictionary on:

  • each instance (referenced via self)
  • a class (also referenced via self, but shared between instances)
  • a module (global, kind of)

depending on what made the most sense given the rest of your code.

Matt Anderson
You understood exactly what i wanted, my primary mistake was that i used the class variant, where i needed the module one, thanks for making me realise that
FrederikNS