con*_*xyz 7 python ipython jupyter
我实现了自己的 __getattr__()的松散处理任何不存在的属性。
我碰巧在 Jupyter 笔记本中定义了这个类,以交互方式对其进行试验。
IPython_ipython_canary_method_should_not_exist_由于这种__getattr__实现而创建-我想了解这是什么以及如何“清理”它是可能的。
有一个关于它的问题,但我不清楚为什么 - 如果它检查内部的许可处理__getattr__- 它没有检查是否在 OP 的示例中实现了_repr_html_ ?
from types import SimpleNamespace
class Metabox(SimpleNamespace):
def __getattr__(self, name):
"""
Create a new namespace for any non-existing attributes.
"""
print("calling __getattr__")
self.__dict__[name] = Metabox()
return self.__dict__[name]
Run Code Online (Sandbox Code Playgroud)
正如上面评论中提到的锥度;IPython 有时需要检查对象以了解它们是否具有某些方法。
处理像你这样的对象;总是说“是”,当我们有疑问时,我们会探索这个名字;如果对象说“是的,我明白了!” 我们知道对象所在并实现了一个__getattr__.
| 归档时间: |
|
| 查看次数: |
1631 次 |
| 最近记录: |