(An Unofficial) Python FAQ Wiki

putting the community back in "maintained by the community"

Is it possible to write obfuscated one-liners in Python?

Yes. Usually this is done by nesting lambda within lambda. See the following three examples, due to Ulf Bartelt:

# Primes < 1000
print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))

# First 10 Fibonacci numbers
print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
range(10))

# Mandelbrot set
print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y
>=4.0) or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr(
64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
#    \\_\__ \_\__  \\_\__ \_\__  |   |   |\_\_ lines on screen
#        V          V      |   |\_\_\_\_\_\_ columns on screen
#        |          |      |\_\_\_\_\_\_\_\_\_\_ maximum of "iterations"
#        |          |\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\__ range on y axis
#        |\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ range on x axis

Don't try this at home, kids!

CATEGORY: programming

Comments

More examples, by Gerson Kurz:

<http://p-nand-q.com/python/obfuscated_python.html>
<http://p-nand-q.com/python/stupid_lambda_tricks.html>
<http://p-nand-q.com/python/lambdaizing_quicksort.html>