Clever hack or code abomination?

Chris Angelico rosuav at gmail.com
Thu Dec 1 21:07:57 EST 2011


On Fri, Dec 2, 2011 at 11:15 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> Try this on for size.
>
>
>                f = type(q)(c[c.index(chr(45))+1:])+type(q)(1)
>                c = str.join('\n', list(map(chr, (45, 48))) + [c])[::2]
>            c = (lambda a,b: a+b)(c[:c.index(chr(45))+1], type(c)(f))

I would consider integer representations of ASCII to be code smell.
It's not instantly obvious that 45 means '-', even if you happen to
know the ASCII table by heart (which most people won't). This is one
thing that I like about C's quote handling; double quotes for a
string, or single quotes for an integer with that character's value.
It's clearer than the Python (and other) requirement to have an actual
function call:

for (int i=0;i<10;++i) {
    digit[i]='0'+i;
    letter[i]='A'+i;
}

versus

for i in range(10):
    digit[i]=chr(ord('0')+i)
    letter[i]=chr(ord('A')+i)

Ignoring the fact that you'd probably use a list comp in Python, this
is imho a win for C.

ChrisA



More information about the Python-list mailing list