We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
gdict
dict
问题模拟代码:
>>> d = gdict() >>> d.a = {} >>> d.a.__class__.__qualname__ 'dict'
我们希望 d.a.__class__.__qualname__ 得到的是 'GqylpyDict'。
d.a.__class__.__qualname__
'GqylpyDict'
The text was updated successfully, but these errors were encountered:
解决方案:重写 __setitem__ 方法,如下代码:
__setitem__
def __setitem__(self, name, value): dict.__setitem__(self, name, GqylpyDict(value))
首先要说明,__setattr__ 方法内部调用 __setitem__。我们将要写入的数据做一次转换,如果数据是 dict 的实例,将被转换为 gdict 实例,包括内层数据。
__setattr__
Sorry, something went wrong.
就在刚刚,我们发现重写 __setitem__ 方法使 deepset 方法失效了,它无法正确深度设置值。进一步排查,问题是出在初始化函数中,重写 __setitem__ 方法让这个问题浮现,关键代码:
deepset
if isinstance(__data__, dict): return dict.__new__(cls)
如果 __dict__ 是 gdict 的实例,这条判断语句也是成立的,因为 gdict 继承 dict。这将会创建新的 gdict 实例,这是错误的,deepset 的底层设计不能创建新的 gdict 实例。为此,我们再次调整代码:
__dict__
if __data__.__class__ is dict: return dict.__new__(cls)
这样做可以有效避免在调用 deepset 时重复创建 gdict 实例的问题,但也引申出新的可能出现的问题:若传入的 __data__ 是其它继承 dict 类的实例,将不会被转换为 gdict 实例。我们暂不考虑使用例如这样的语句来避免此问题:
__data__
if isinstance(__data__, dict) and __data__.__class__ is not gdict: return dict.__new__(cls)
已采用类似上述语句避免此问题,问题关闭。
引发新的问题 #9 ,在 1.2.1 版本已回退。
2018-11-27
No branches or pull requests
问题模拟代码:
我们希望
d.a.__class__.__qualname__
得到的是'GqylpyDict'
。The text was updated successfully, but these errors were encountered: