You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is definitely a documentation update and perhaps a bug or feature request (box and subclasses of box should be by reference instead of copy).
For the documentation, a clear section covering copying vs references would have really helped me.
Here is my proposed documentation update (as I could not figure out how to provide pull request for the wiki:
How Box Handles Conversions
When data is brought into a Box, the values are copied. In particular, when you import a dict or a list, changes to the original dict or list will not be reflected in the Box. In some future version of Box, the references to the values may be preserved, but at the moment, this is not possible.
For any other type, passing "box_intact_types" with a tuple listing the types to preserve will create a reference in the Box instead of a copy. In some future version of Box, subclasses of Box will automatically be considered a "box_intact_type".
The examples below illustrate this:
Box does not create a connection to the original dictionary
from box import Box
from copy import deepcopy
original = {"key1": "value1", "key2": "value2", "key3": "value3"}
original_ref = original
original_copy = deepcopy(original)
box = Box(original)
print(f""" original["key1"] ==> {original["key1"]}""")
# expect "value1"
# change the value in the original
original["key1"] = "new value"
print(f""" original["key1"] ==> {original["key1"]}""")
# expect "newvalue"
print(f""" original_ref["key1"] ==> {original_ref["key1"]}""")
# expect "newvalue"
print(f"""original_copy["key1"] ==> {original_copy["key1"]}""")
# expect "value1"
print(f""" box.key1 ==> {box.key1}""")
# expect "value1"
Produces the following output:
original["key1"] ==> value1
original["key1"] ==> new value
original_ref["key1"] ==> new value
original_copy["key1"] ==> value1
box.key1 ==> value1
Box does not create a connection to imported Boxes
Thanks you for allowing me to contribute! Box is super useful to me and I use it all the time. Really appreciate that you continue to maintain and work on this.
This is definitely a documentation update and perhaps a bug or feature request (box and subclasses of box should be by reference instead of copy).
For the documentation, a clear section covering copying vs references would have really helped me.
Here is my proposed documentation update (as I could not figure out how to provide pull request for the wiki:
How Box Handles Conversions
When data is brought into a Box, the values are copied. In particular, when you import a dict or a list, changes to the original dict or list will not be reflected in the Box. In some future version of Box, the references to the values may be preserved, but at the moment, this is not possible.
For any other type, passing "box_intact_types" with a tuple listing the types to preserve will create a reference in the Box instead of a copy. In some future version of Box, subclasses of Box will automatically be considered a "box_intact_type".
The examples below illustrate this:
Box does not create a connection to the original dictionary
Produces the following output:
Box does not create a connection to imported Boxes
Produces the following output:
Box does not create a connection to subclasses of Box
produces the following output:
Use
box_intact_types
to create a connection to subclasses of BoxProduces the following output:
The text was updated successfully, but these errors were encountered: