+ 1
A detail about class variables in Python
Okay, I have an issue that sounds a bit hard to google, but maybe we've got a good pythonist here who will be able to quickly help me. Imagine I have a datatype with a class variable that tracks the number of created instances. Now from my application I am creating two collections of that type and save them with pickle or something. The question: Will each separately stored collection have it's own running number - or will the number always be starting over at zero when the program is started, and then count up until I exit the run, no matter how many collections I work with? Finally, if you wanted each collection to get its own running number, what would be a good "pythonic" implementation for that?
3 Antworten
+ 6
I had a little trouble sorting out your question in my old brain. But I posted it to my AI and this is the response. Maybe it's helpful. :)
When you serialize and deserialize objects using pickle, the class variable that tracks the number of created instances will not be reset to zero. Instead, it will continue to count up from where it left off in the current program run. This is because class variables are shared among all instances of the class and are not reset upon deserialization. If you want each collection to have its own running number, you can implement an instance variable to track the number of instances within each collection. Here is a "pythonic" implementation:
Define a class with an instance counter.
Create a collection class that manages instances of the first class and maintains its own counter.
Here is an example:
https://www.sololearn.com/en/compiler-playground/cHlRvaA0SAJy
In this implementation:
MyClass is the class whose instances are being tracked.
Collection is a class that manages instances of MyClass and maintains its own counter (instance_counter).
Each Collection object has its own counter, ensuring that instance IDs are unique within each collection.
The __getstate__ and __setstate__ methods ensure that the instance counter is correctly serialized and deserialized.
+ 2
Jerry Hobby
nice 👍
I played around with your code, removed and added some methods to hopefully better explore what's going on. Yes, pickling preserves classes at their state at the time they are pickled.
about the __getstate__, __setstate__, correct me if I'm wrong, but I gather that they should be defined only if you are using __slots__ in your class because your class won't have access to .__dict__. I omitted __getstate__ and __setstate__ and pickle still worked ok.
https://stackoverflow.com/questions/1939058/simple-example-of-use-of-setstate-and-getstate
As for a Pythonic way to count the number of instances, you can just get the len of your instances list. This would remove the disjoint between the list and counter and reduce the variables you need to keep track of.
But I feel that this concept, while sound, is more trouble to implement than just simply incrementing a counter. Use what works.
https://sololearn.com/compiler-playground/cPNZgZDx4POJ/?ref=app
+ 1
I'm not on the top today (soaked to the skin when I went shopping), but I think you need a thread