在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?

请每个回答只回答一个特征。


当前回答

早期的FORTRAN,空格不重要。(anti-Python !)

DO 20 I = 1, 10

含义:从这里循环到第20行,I从1到10。

DO 20 I = 1. 10

含义:将1.10分配给名为DO20I的变量。

有传言说这个漏洞毁了一个太空探测器。

其他回答

在Python中,函数参数的“编译时”(或声明时)计算可能令人困惑:

def append(v, l = []):
    l.append(v)
    return l


print append(1)
print append(2)

>>> [1]
>>> [1,2]

其意图可能是:

def append(v, l = None):
    if l is None:
        l = []
    l.append(v)
    return l

print append(1)
print append(2)

>>> [1]
>>> [2]

这种行为对于缓存之类的事情很有用,但它可能是危险的。

附加特性:具有可变内容的元组:

a = (1,2,[3])
a[2][:] = [4] # OK
a[2] = [2] # crashes

PHP对字符串中数值的处理。详见之前对另一个问题的回答,但简而言之:

"01a4" != "001a4"

如果你有两个包含不同数量字符的字符串,它们不能被认为是相等的。前导零很重要,因为它们是字符串而不是数字。

"01e4" == "001e4"

PHP doesn’t like strings. It’s looking for any excuse it can find to treat your values as numbers. Change the hexadecimal characters in those strings slightly and suddenly PHP decides that these aren’t strings any more, they are numbers in scientific notation (PHP doesn’t care that you used quotes) and they are equivalent because leading zeros are ignored for numbers. To reinforce this point you will find that PHP also evaluates "01e4" == "10000" as true because these are numbers with equivalent values. This is documented behaviour, it’s just not very sensible.

在javaScript中,NaN是一个全局变量。

转变;

有时你会在perl方法的第一行中看到它来读取self指针

Python 2。X演示了一个糟糕的列表理解实现:

z = 4
s = [z*z for z in range(255)]
print z

这段代码返回254。列表推导式的变量与上定义的变量冲突。

Python 3。x已经处理了这个特性,但是闭包仍然对外部变量使用动态链接,并在函数式python编程器中引入了许多wtf

def mapper(x):
    return x*x
continuations = [lambda: mapper(x) for x in range(5)]
print( [c() for c in continuations])

此代码显然返回[16,16,16,16,16]。