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

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


当前回答

不知道这是不是一个功能。对一些人来说,是的,但对另一些人来说,这可能是一种令人讨厌的行为。不管怎样,我认为这是值得一提的。

在Python中,内置函数round()在Python 2x和Python 3x之间的行为略有不同。

对于Py 2x,

>>> round(0.4)
0.0
>>> round(0.5)
1.0
>>> round(0.51)
1.0
>>> round(1.5)
2.0

对于Py 3x,

>>> round(0.4)
0
>>> round(0.5)
0
>>> round(0.51)
1
>>> round(1.5)
2

我只是不熟悉Py 3x中的round()与0的工作方式。

Py 2x和Py 3x中round()的文档。

其他回答

Perl的许多内置变量:

$# — not a comment! $0, $$, and $? — just like the shell variables by the same name $ˋ, $&, and $' — weird matching variables $" and $, — weird variables for list- and output-field-separators $! — like errno as a number but strerror(errno) as a string $_ — the stealth variable, always used and never seen $#_ — index number of the last subroutine argument... maybe @_ — the (non)names of the current function... maybe $@ — the last-raised exception %:: — the symbol table $:, $^, $~, $-, and $= — something to do with output formats $. and $% — input line number, output page number $/ and $\ — input and output record separators $| — output buffering controller $[ — change your array base from 0-based to 1-based to 42-based: WHEEE! $} — nothing at all, oddly enough! $<, $>, $(, $) — real and effective UIDs and GIDs @ISA — names of current package’s direct superclasses $^T — script start-up time in epoch seconds $^O — current operating system name $^V — what version of Perl this is

还有很多这样的东西。点击这里阅读完整列表。

在c++中,“虚”MI(多重继承)允许“菱形”类层次结构“工作”,这让我觉得奇怪和讨厌。

A:基类,例如:“对象” B, C:两者都(实际上或不是)源于对象和 D:起源于B和C

问题:“正常”继承导致D是2种不明确的A。“虚拟”MI将B的A和C的A折叠为一个共享基对象A。

所以,即使你的车轮是一个对象,你的左前轮是一个车轮,你的汽车继承了四种车轮,你的汽车仍然只是一种具有虚拟MI的对象。否则,你的汽车不是一个对象,而是4个车轮对象。

这是一种奖励糟糕的类设计、惩罚编译器编写者的语言特性,并让您在运行时怀疑对象到底在哪里——以及是否有任何虚拟MI行李放错了地方。

如果在类层次结构中确实需要菱形模式,可以使用常规MI和委托给单个A基的“AProxy”来完成。

A:基类,例如:“对象” AProxy:基类,与其他A绑定的构造 B:源自A C:来源于AProxy D:源自B和C(在构造时将B的A传递给C的AProxy)

对于那些真正喜欢diamond MI的人来说,这需要做更多的工作,而我们其他人则可以安心地使用一组更易于处理的语言特性。

叫/ cc。Call/cc将一个表示程序其余部分的函数传递给程序体。

c# yield语句,并不奇怪,但非常有用。

http://msdn.microsoft.com/en-us/library/9k7k7cf0 (VS.80) . aspx

在JavaScript中:

var something = 12;

function nicelyCraftedFunction()
{
  something = 13;
  // ... some other code
  // ... and in Galaxy far, far away this:
  if( false ) // so the block never executes:
  { 
    var something; 
  }
}
nicelyCraftedFunction(); // call of the function

通常你会期望某个变量的值是13。 但在JavaScript中不是这样——变量有函数作用域,所以后面的声明会影响上游的一切。

在使用C/ c++ /Java表示法的语言(如JS)中,你会期望变量具有块范围,而不是像这样…

因此,编译器甚至可以从最终生成的字节码中删除的死代码块仍然会对正常执行的其余代码产生副作用。

因此,在调用函数之后,某些东西仍然是12 -不变的。