This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pickle not working correctly when custom field is directly initialized by constructors
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, jeffersonqin
Priority: normal Keywords:

Created on 2022-03-16 14:24 by jeffersonqin, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test.py jeffersonqin, 2022-03-16 14:24 Demo file
Messages (4)
msg415341 - (view) Author: (jeffersonqin) Date: 2022-03-16 14:24
For the following code piece:

```
import pickle

class Child:
	def __init__(self, field):
		self.field = field

class Parent:
	child = Child(0.5)

if __name__ == '__main__':
	i = input()
	if i == 'd':
		parent = Parent()
		parent.child.field = 0.6
		pickle.dump(parent, open('test.pkl', 'wb+'))
	else:
		parent = pickle.load(open('test.pkl', 'rb'))
		print(parent.child.field)
```

After dumping, when we load the file throught `pickles.load`, `parent.child.field` is 0.5, and is not 0.6, which we intend it to be.

However, after removing the line `child = Child(0.5)` and moving it to `__init__(self)` of `Parent`, everything works fine.

I'm not sure whether this is indeed an issue. If not, sorry for take your time.
msg415345 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-16 15:13
child is not an attribute of a Parent instance, but rather of the Parent class. So it's not going to be saved when you dump "parent", which is an instance of Parent.

I'm not sure what you're doing when you create a Parent.__init__ method, but presumably setting self.child, which will then create an instance attribute which will get saved by pickle.
msg415348 - (view) Author: (jeffersonqin) Date: 2022-03-16 15:52
Thanks! This fully answers my question. Sorry, but I've got another question regards to your answer. It is that when I deal with other fields in class Parent similar to child, with the only difference is that their types are builtin types such as integer and boolean, everything works fine and can be correctly serialized after been modified. What's the difference between these two cases?
Again, sorry for disturbing and thanks a lot for your detailed answer.
msg415350 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-16 15:58
You're probably doing something like:

parent.i = 3

instead of:

parent.child.field = 0.6

In the first one, you're setting an instance attribute on parent, on the second, you're modifying an attribute of the class attribute.

In any event, there's no bug here, so I'm going to close this. If you have followup questions, I suggest you use the python-list mailing list or https://discuss.python.org/c/users
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91190
2022-03-16 15:58:37eric.smithsetstatus: open -> closed
resolution: not a bug
messages: + msg415350

stage: resolved
2022-03-16 15:52:49jeffersonqinsetmessages: + msg415348
2022-03-16 15:13:56eric.smithsetnosy: + eric.smith
messages: + msg415345
2022-03-16 14:24:35jeffersonqincreate