Chinaunix首页 | 论坛 | 博客
  • 博客访问: 517690
  • 博文数量: 230
  • 博客积分: 5726
  • 博客等级: 大校
  • 技术积分: 2765
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-21 13:51
文章分类

全部博文(230)

文章存档

2011年(33)

2010年(40)

2009年(157)

分类: Python/Ruby

2009-05-25 10:54:45

本文转自:
 

1 有趣的例子

1.1 看不懂

Python 的代码以可读性强著称, 不过如果你足够变态的话, 还是能够写出很可怕的代码的。 Ulf Bartelt 就写了这些可怕的代码:

小于 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)))

开头 10 个 Fibonacci 数:

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 集:

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 

小孩子千万别在自己家里乱试!

 

1.2 无穷递归现象

看这段代码:

[edward@argos Viki]$ python
Python 2.4.1 (#1, Apr 12 2005, 15:37:40)
[GCC 3.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> GNU=["GNU","'s not", "Unix"]
>>> GNU[0]=GNU
>>> GNU
[[...], "'s not", 'Unix']
>>> GNU[0]
[[...], "'s not", 'Unix']
>>> GNU[0][0]
[[...], "'s not", 'Unix']
>>> GNU[0][0][0]
[[...], "'s not", 'Unix']
>>> GNU[0][0][0][0]
[[...], "'s not", 'Unix']
>>> GNU[0][0][0][0][0]
[[...], "'s not", 'Unix']
>>> GNU[0][0][0][0][0][0]
[[...], "'s not", 'Unix']
>>> GNU[0][0][0][0][0][0][0]
[[...], "'s not", 'Unix']
>>>

好像有点可怕啊……

2 乱七八糟的 Python

2.1 乱七八糟的逻辑常量

看看下面的代码片段, 貌似 Python 根本就没把 True、False 当作逻辑常量来处理。

Python 2.4.1 (#1, Apr 12 2005, 15:37:40)
[GCC 3.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> True = False
>>> print False
False
>>> print True
False
>>>

怎么办? 怎么办? 天下大乱了!

 原文地址
阅读(643) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~