-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use the dict() function to convert an easydict object to a regular dictionary #48
Comments
If we use this easydict related data to send the function to the send function of the request function, it may also cause problems |
Alternatively, I would expect to provide a method for this class, such as XX. change_to_dict(), and then help me switch back to a regular dictionary |
I can't help it, I was too eager to use it, so I wrote the method for rebuilding the dictionary myself. Can you also let me join this project so that I can provide the relevant code The code is as follows
|
class EasyDict(dict):
if name == "main":
|
You can use
first override def __reduce__(self):
return dict, (self.__dict__,) and you can now ezdict = EasyDict(items=[EasyDict(items='something')])
from copy import deepcopy
ezdict = deepcopy(ezdict) and get a copied instance with every In your implementation, you define a new instance method To avoid such side effect, you may handle it in a magic method way def __reduce__(self):
return dict, (self.__dict__,)
def __asdict__(self, recurse=True):
if recurse:
from copy import deepcopy
return deepcopy(self.__dict__)
else:
return dict(self.__dict__) and use it like: dct = ezdict.__asdict__() for type reversion. Note: overriding If def __asdict__(self, recurse=True):
if recurse:
_r = self.__class__.__reduce__
self.__class__.__reduce__ = lambda obj: (dict, (obj.__dict__,),)
from copy import deepcopy
_c = deepcopy(self.__dict__)
self.__class__.__reduce__ = _r
return _c
else:
return dict(self.__dict__) |
It looks very good code. Thank you very much for your patient guidance. I will read this code a few more times |
I feel very happy being answered on GitHub. Thank you |
I just discovered a problem, which is that if the value of a key in this data object is an io_buffer, an error will occur, |
Emmm, the solution is probably to make a targeted judgment. You can't use dict directly. Let me see how to change it. |
#############################################
I think it will be a problem that needs to be solved because it is not possible to revert back to a regular dict, which may result in certain scenarios where other third-party libraries cannot be used. Because of the code design, sometimes I may need to first convert it to a regular dictionary, process it, and then revert back
The text was updated successfully, but these errors were encountered: