Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当在Chameleon ZPT模板中找不到字典键时,有没有办法抑制键错误?_Python_Template Tal_Chameleon_Keyerror - Fatal编程技术网

Python 当在Chameleon ZPT模板中找不到字典键时,有没有办法抑制键错误?

Python 当在Chameleon ZPT模板中找不到字典键时,有没有办法抑制键错误?,python,template-tal,chameleon,keyerror,Python,Template Tal,Chameleon,Keyerror,有时,如果能够做到这一点,只需在对不存在的dict键求值的任何表达式中返回None,而不是引发KeyError即可。您可以使用以下方法来访问该键: <div tal:attributes="class some_dict.get(some_key)"> 如果某个键不存在,则类属性将被忽略,因为dict.get()的默认返回值是无,如果缺少键。只需使用try/except语句捕捉错误。@wwii:这是一种模板语言,不是Python.Hmm,我的错,很抱歉get()可能是正确的响应

有时,如果能够做到这一点,只需在对不存在的dict键求值的任何表达式中返回None,而不是引发KeyError即可。您可以使用以下方法来访问该键:

<div tal:attributes="class some_dict.get(some_key)">


如果
某个键不存在,则
属性将被忽略,因为
dict.get()
的默认返回值是
,如果缺少键。

只需使用try/except语句捕捉错误。@wwii:这是一种模板语言,不是Python.Hmm,我的错,很抱歉
get()
可能是正确的响应,即使它不是模板语言。