@@ -116,6 +116,7 @@ Here's an overview of the questions per chapter:
116116 ImportError: No module named ...; what gives?
117117 4.11. Q. What GUI toolkits exist for Python?
118118 4.12. Q. Are there any interfaces to commercial database in Python?
119+ 4.13. Q. Is it possible to write obfuscated one-liners in Python?
119120
120121 5. Extending Python
121122 5.1. Q. Can I create my own functions in C?
@@ -683,6 +684,35 @@ A. There's an interface to SYBASE by John Redford
683684There's also an interface to metalbase by Lance Ellinghouse
684685685686
687+ 4.13. Q. Is it possible to write obfuscated one-liners in Python?
688+
689+ A. Yes. See the following three examples, due to Ulf Bartelt:
690+
691+ # Primes < 1000
692+ print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,\
693+ map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))
694+
695+ # First 10 Fibonacci numbers
696+ print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),\
697+ range(10))
698+
699+ # Mandelbrot set
700+ print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,\
701+ Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,\
702+ Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,\
703+ 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\
704+ >=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(\
705+ 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy\
706+ ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
707+ # \___ ___/ \___ ___/ | | |__ lines on screen
708+ # V V | |______ columns on screen
709+ # | | |__________ maximum of "iterations"
710+ # | |_________________ range on y axis
711+ # |____________________________ range on x axis
712+
713+ Don't try this at home, kids!
714+
715+
686716
6877175. Extending Python
688718===================
0 commit comments