码迷,mamicode.com
首页 > 其他好文 > 详细

模板点操作符查找顺序,调用方法

时间:2015-03-19 19:54:25      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

字段,属性,方法,列表

方法不被调用

>>> class Test(object):
	def delete(self):
		return ‘echo from delete...‘
	delete.alters_data=True

	
>>> t.render(Context({‘t‘:Test()}))
u‘‘

 

在方法查找过程中,如果某方法抛出一个异常,除非该异常有一个 silent_variable_failure 属性并且值为 False ,否则的话它将被传播。设置为True,异常不被传播,模板里的指定变量会被 置为空字符串,比如:

>>> t = Template("My name is {{ person.first_name }}.")
>>> class PersonClass3:
...     def first_name(self):
...         raise AssertionError, "foo"
>>> p = PersonClass3()
>>> t.render(Context({"person": p}))
Traceback (most recent call last):
...
AssertionError: foo

>>> class SilentAssertionError(AssertionError):
...     silent_variable_failure = True
>>> class PersonClass4:
...     def first_name(self):
...         raise SilentAssertionError
>>> p = PersonClass4()
>>> t.render(Context({"person": p}))
u‘My name is .‘

 

模板点操作符查找顺序,调用方法

标签:

原文地址:http://www.cnblogs.com/Citizen/p/4351132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!