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

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


当前回答

试试吧,除非,否则

try:     pass
except:  pass
else:    pass
finally: pass

如果没有捕获异常,则执行else部分。

有道理,但一开始我真的不知道它是干什么的。

例子:

def show_square(string):
  try:
    n = int(string, 10)
  except ValueError:
    print "I can't do that, Dave."
  else:
    print n * n

其他回答

JavaScript是面向对象的,对吧?因此,在文字字符串和数字上运行方法应该是可行的。比如"hello". touppercase()和3.toString()。第二个是语法错误,为什么?因为解析器期望一个数字后面跟一个点是一个浮点字面值。这不是WTF, WTF是你只需要再加一个点就可以了:

3..toString()

原因是字面上的3。被解释为3.0,3.0. tostring()工作正常。

在JavaScript中,undefined是一个全局变量,其默认值为原始值undefined。你可以改变undefined的值:

var a = {};
a.b === undefined; // true because property b is not set
undefined = 42;
a.b === undefined; // false

由于undefined的可变性,通过typeof检查undefined通常是一个更好的主意:

var a = {};
typeof a.b == "undefined"; // always true

这个古老的PHP宠儿本身并不完全是WTFish,但作用域解析错误是许多开发人员看到的事情之一,值得给一些WTF的爱:

$class = new StdClass();
$class::test();

PHP Parse error:  syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM on line 3

这是我的两分钱。在c++中:

int* t = new int(15);
delete t;

PHP

来自在线文档: string implode (string $glue, array $pieces) -用字符串连接数组元素 注意:由于历史原因,implode()可以以任意一种顺序接受其形参。

这是可行的:implode($someArray, $glue)

希望他们能在PHP 6中消除这些历史怪癖。