[issue47034] pickle not working correctly when custom field is directly initialized by constructors

2022-03-16 Thread jeffersonqin


New submission from jeffersonqin <1247006...@qq.com>:

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.

--
components: Library (Lib)
files: test.py
messages: 415341
nosy: jeffersonqin
priority: normal
severity: normal
status: open
title: pickle not working correctly when custom field is directly initialized 
by constructors
type: behavior
versions: Python 3.9
Added file: https://bugs.python.org/file50682/test.py

___
Python tracker 
<https://bugs.python.org/issue47034>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47034] pickle not working correctly when custom field is directly initialized by constructors

2022-03-16 Thread jeffersonqin


jeffersonqin <1247006...@qq.com> added the comment:

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.

--

___
Python tracker 
<https://bugs.python.org/issue47034>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com