IPython“金丝雀方法”的含义以及如果它存在会发生什么?

2024-05-15 01:36:55 发布

您现在位置:Python中文网/ 问答频道 /正文

IPython源代码包括a ^{} check,它在get_real_method函数的开头测试'_ipython_canary_method_should_not_exist_'是否存在:

def get_real_method(obj, name):
    """Like getattr, but with a few extra sanity checks:
    - If obj is a class, ignore everything except class methods
    - Check if obj is a proxy that claims to have all attributes
    - Catch attribute access failing with any exception
    - Check that the attribute is a callable object
    Returns the method or None.
    """
    try:
        canary = getattr(obj, '_ipython_canary_method_should_not_exist_', None)
    except Exception:
        return None

    if canary is not None:
        # It claimed to have an attribute it should never have
        return None

尽管很容易让其他编码人员将这个名称特殊地加上find,但是很难找到任何有意义的解释。在

考虑到这两类:

^{pr2}$

IPython似乎是在利用这个方法的存在与否来决定是使用repr还是其中的一个{a3}。有意阻止DeadParrot中的测试会导致IPython查找并调用_repr_mimebundle_。在

我在写一个对象,假装所有属性都存在。我怎么决定是否要特例?在


Tags: noneobjgetishaveipythonnotattribute

热门问题