Code:
PEP 292 adds a Template class to the string module that uses "$" to
indicate a substitution. Template is a subclass of the built-in
Unicode type, so the result is always a Unicode string:
[color=blue][color=green][color=darkred]
>>> import string
>>> t = string.Template('$page: $title')
>>> t.substitute({'page':2, 'title': 'The Best of Times'})[/color][/color][/color]
u'2: The Best of Times'
If a key is missing from the dictionary, the substitute method will
raise a KeyError. There's also a safe_substitute method that ignores
missing keys:
[color=blue][color=green][color=darkred]
>>> t = string.SafeTemplate('$page: $title')
>>> t.safe_substitute({'page':3})[/color][/color][/color]
u'3: $title'
Code:
As a slightly more realistic example, the following decorator checks that the supplied argument is an integer: def require_int (func): def wrapper (arg): assert isinstance(arg, int) return func(arg) return wrapper @require_int def p1 (arg): print arg @require_int def p2(arg): print arg*2
Serioulsy, one of python's main selling points is its elegant syntax,
non perl like, non C like. If it can't live up to it. I guess i might
as well use perl or ruby or server side javascript.
how annoying.
Comment