最接近常规 Python 行为的是 StrictUndefined ,如果它是一个未定义对象,它不允许除了测试之外的一切操作。
- _class _jinja2.Undefined
- The default undefined type. This undefined type can be printed anditerated over, but every other access will raise an UndefinedError:
- _undefined_hint
None 或给未定义对象的错误消息 unicode 字符串。
未定义变量/属性的名称,如果没有此类信息,留为 None 。
_undefined_exception
- 参数任意,调用这个方法时会抛出带有由未定义对象上存储的未定义hint 生成的错误信息的 异常。
- An undefined that barks on print and iteration as well as booleantests and all kinds of comparisons. In other words: you can do nothingwith it except checking if it’s defined using the defined test.
未定义对象由调用 创建。
实现
要禁用一个方法,重载它并抛出 _undefined_exception 。因为这在未定义对象中非常常用,未定义对象有辅助方法 自动抛出错误。这里的一个类工作类似正规的 Undefined ,但它在迭代时阻塞: