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

2022-03-16 Thread Eric V. Smith


Eric V. Smith  added the comment:

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

--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
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 

___
___
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 Eric V. Smith


Eric V. Smith  added the comment:

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.

--
nosy: +eric.smith

___
Python tracker 

___
___
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


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 

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