Python's annoyance.
Steven Bethard
steven.bethard at gmail.com
Wed Nov 24 14:03:26 EST 2004
caroundw5h wrote:
> 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.
While I'm not a fan of the decorator syntax or the $syntax in
string.Template, you're not forced to use either of these:
Standard Python formatting vs string.Template formatting:
------------------------------------------------------------
>>> string.Template('$page: $title').substitute(dict(page=2, title='The
Best of Times'))
'2: The Best of Times'
>>> '%(page)s: %(title)s' % dict(page=2, title='The Best of Times')
'2: The Best of Times'
Post-function syntax vs. decorator syntax:
------------------------------------------------------------
>>> def require_int(func):
... def wrapper(arg):
... assert isinstance(arg, int)
... return func(arg)
... return wrapper
...
>>> @require_int
... def p1(arg):
... print arg
...
>>> def p2(arg):
... print arg
...
>>> p2 = require_int(p2)
>>> p1(1)
1
>>> p1('1')
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 3, in wrapper
AssertionError
>>> p2(1)
1
>>> p2('1')
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 3, in wrapper
AssertionError
Personally, I never saw the gain of the string.Template class -- in most
of my uses I can write code that's just as clear and concise using
%-style formatting. I do however make use of the decorator syntax --
it's nice to have things like classmethod indicated at the top of the
function instead of at the bottom.
Steve
More information about the Python-list
mailing list