Clever hack or code abomination?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Dec 1 19:15:55 EST 2011


On Thu, 01 Dec 2011 21:13:51 +0000, Martin P. Hellwig wrote:

> On 01/12/2011 03:15, Roy Smith wrote: <cut>
> Well, I have seen much worse, so the WTFs/minute(*) count won't be too
> bad.
> 
> However, as  general rule for readability; If you think you have to ask,
> don't bother asking, spend that time rethinking and write a more
> readable solution.

That's generally good advice, but in this case, I don't know why Roy 
Smith thought he had to ask. His initial snippet was pretty much standard 
vanilla Python: a for loop over a list. The list was made by adding a 
list containing a single element '', and a second list made from a list 
comprehension that converted a bunch of numbers into strings. It uses no 
advanced features like generators. A newbie to Python could work it out.

In my opinion, if anyone thinks that's "obfuscated" (as opposed to merely 
"not quite idiomatic"), either their standards for readability are 
impossibly high, or they just can't read Python code.

Try this on for size.


def obfuscated_prefixes(q=20):
    c = str()
    while isinstance(type(c), type(type)):
        yield None or c
        while isinstance(c, object):
            try:
                f = type(q)(c[c.index(chr(45))+1:])+type(q)(1)
            except ValueError:
                c = str.join('\n', list(map(chr, (45, 48))) + [c])[::2]
            else:
                break
        if f <= q: pass
        else: break
        try:
            c = (lambda a,b: a+b)(c[:c.index(chr(45))+1], type(c)(f))
        except IndexError:
            c = c[1::]
    


-- 
Steven



More information about the Python-list mailing list