
今天在微信 你见过最垃圾的代码长什么样?[1] 看到这篇趣文,它最初来自于 CSDN中的博文[2] 。
01 import this
运行下面Python代码之后:
import this
你将会得到:
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
02 创造新轮子
如果常规的逻辑判断不复合我的要求,我就创造一个信号的判断函数。

03 神奇的数字
看到这幅图片中的代码,我不禁要问:如果改变了其中一个数字会发生什么呢?可能没有什么好的事情。

04 机器C语音
这也许就是给计算机写的C语言。如果是给C语言老师提交的课程作业,我向这位老师深表同情和慰问。
也许这是从别的文本编辑器(利用 "\n"表示新行)拷贝到需要"\r\n"表示新行的编辑器的结果


05 判断奇偶数
这应该是判断一个数字的奇偶性的最Naive的算法了。

不过这个函数让我想起了一个中国笑话:一个刚刚学会汉字 一、二、三 写法的人考虑如何把他的姓名(他姓“万”)写在书本上。
我认真编写了下面的代码测试,上面的代码除了工作效率之外一切都很正常。
import sys,os,math,time
import matplotlib.pyplot as plt
from numpy import *
def isEven(n):
n = abs(n)
rst = True
for i in range(n):
rst = not rst
return rst
starttime = time.time()
print("isEven(100000000): {}".format(isEven(100000000)), "isEven(100000001): {}".format(isEven(100000001)))
elapsetime = time.time()-starttime
print("elapsetime: {}".format(elapsetime))
isEven(100000000): True
isEven(100000001): False
elapsetime: 4.847123622894287

06 字符串长度
当他有了获取一个字符串长度的需要的时候,没有犹豫一秒钟就立即实现了它。

07 Emoji变量
看看下面代码中,使用表情包(Emoji)符号作为变量名,程序编写的挺感人的。不是吗?

测试了一下Python中使用这些额外的字符情况,看来上面代码是想多了。
○ = 1
print(○)
● = 'ball'
print("●: {}".format(●))
运行结果呢:
File "/tmp/ipykernel_131/3180195851.py", line 5
○ = 1
^
SyntaxError: invalid character in identifier
08 超宽的屏幕
现在我终于明白了为什么编程人员喜欢宽屏幕了。但有一个问题:到底屏幕多宽才最终够用?

09 其实不可笑
起初看到这个函数觉得挺可笑的。不过对于运行在某些单片机上MicroPython语言来说,有可能最基础的运算它的确不支持。比如对于小数运算,在MicroPython中并不是缺省支持的。需要自行编写相应的浮点,或者定点小数运算。

参考资料
你见过最垃圾的代码长什么样?: https://mp.weixin.qq.com/s/UyVG-Pn_S3I3zIoefcLcgw
[2]CSDN中的博文: https://blog.csdn.net/daocaokafei/article/details/120733959