Expand|Select|Wrap|Line Numbers
- 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:
-
- import string
- t = string.Template('$page: $title')
- t.substitute({'page':2, 'title': 'The Best of Times'})
- 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:
-
- t = string.SafeTemplate('$page: $title')
- t.safe_substitute({'page':3})
- u'3: $title'
Expand|Select|Wrap|Line Numbers
- 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.