Skip to content
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

是否可以做到 gdict 覆盖内置 dict #3

Open
2018-11-27 opened this issue Aug 2, 2022 · 2 comments
Open

是否可以做到 gdict 覆盖内置 dict #3

2018-11-27 opened this issue Aug 2, 2022 · 2 comments
Assignees
Labels
question Further information is requested UnableProcess

Comments

@2018-11-27
Copy link
Member

gdict 覆盖内嵌 dict,这样以后在全局创建例如 {'a': 'A'} 即可直接得到 gdict 实例,而无需再做转换。

@2018-11-27 2018-11-27 self-assigned this Aug 2, 2022
@2018-11-27 2018-11-27 added the question Further information is requested label Aug 2, 2022
@2018-11-27
Copy link
Member Author

初步尝试,从 builtins 中篡改 dict。尝试未成功,如下代码:

>>> import builtins
>>> import gqylpy_dict
>>> builtins.dict = gqylpy_dict.gdict

此时查看 dict,出现一个奇特的现象:

>>> dict.__qualname__
'GqylpyDict'
>>> dict().__class__.__qualname__
'dict'
>>> {}.__class__.__qualname__
'dict'

全局 dict 已经指向 gdict,但调用全局 dict 得到的却仍是内嵌 dict 的实例。这样看来,调用全局 dict 并不是调用 builtins.dict。Python是一门高深的语言,你永远也猜不到它的底层设计。

@2018-11-27
Copy link
Member Author

尝试方案二,从 globals 中篡改 dict

>>> import gqylpy_dict
>>> globals()['dict'] = gqylpy_dict.gdict

此时查看 dict

>>> dict.__qualname__
'GqylpyDict'
>>> dict().__class__.__qualname__
'GqylpyDict'
>>> {}.__class__.__qualname__
'dict'

看起来比上一次成功,全局 dict 已经指向 gdict,调用全局 dict 也得到 gdict 实例,但使用 {} 的方式
仍得到内嵌 dict 实例。

@2018-11-27 2018-11-27 changed the title 是否可以做到gdict覆盖内嵌dict 是否可以做到 gdict 覆盖内置 dict May 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested UnableProcess
Projects
None yet
Development

No branches or pull requests

1 participant