IPython源代码包括a ^{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_
。在
我在写一个对象,假装所有属性都存在。我怎么决定是否要特例?在
目前没有回答
相关问题 更多 >
编程相关推荐