I am trying to develop a python class and would like to use the new magic method for object creation. It doesn't seem to be called when a new instance is created. The init method is called but new is not. I have a feeling this is a difference in the pure python execution vs. the jython used for Ignition scripting. I am reaching out to the community for suggestions or confirmation if my suspicions are correct.
What I am trying to accomplish is during instance creation if the ID value passed to the constructor of the class is present in a cache dictionary, pass back the already created instance. Otherwise create a new instance.
Does your class inherit from "object", to make it a new style class? That's required in Python 2.
Thank you for properly addressing the XY problem
This way lies danger. I don't know what cache you're using or exactly how, but it is unsafe (read: causes memory leaks) to store user defined Jython classes between script interpreter restarts, which Ignition does all the time, under the hood. I would urge you to cache whatever expensive data or state you need as pure Python dicts/lists with other primitives into that cache and reconstitute your class based on that, rather than trying to persist instances.
Not sure it is a good plan anyways, as jython object IDs are not unique in Ignition, at least in the gateway. Different interpreter instances maintain their own, IIRC.
Just for my understanding, caching user defined class instances is a cause for memory leaking because the class definition, the instance is based on, gets destroyed on an interpreter restart? When I go to retrieve the instance from the cache, assuming it didn't get destroyed also, it will basically be orphaned memory that has no definition? It will never get garbage collected because the cache is holding a reference to the memory address.
No, it gets left there, and will still run old code. And its references into the old interpreter keep the entire old interpreter from being garbage collected. This is also true for any long-lived asynchronous thread--it will keep running old code, and holding onto its interpreter, until it exits normally.
Understand. Thank you for the clarification. A co-worker of mine was just mentioning the other day about long running async scripts and the running old code issue.