在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
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.
其他回答
在流行性腮腺炎中,你可以有一个带偏移的GOTO。如果你有(我的腮腺炎已经生锈了…)
some_label if x=1 do_something
else do_something_else
然后是代码
goto some_label+1
将跳转到ELSE语句…
这个C程序在x86和x86-64上输出不同的结果:
#include <stdio.h>
int main (void)
{
long a = -1;
unsigned b = 1;
printf ("%d\n", a > b);
return 0;
}
不知道这是不是一个功能。对一些人来说,是的,但对另一些人来说,这可能是一种令人讨厌的行为。不管怎样,我认为这是值得一提的。
在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的CORE::开放和标准库具有面向对象的元素,用一个过程性接口掩盖: Open (my $fh, '>', 'foobar'); Open是一个构造函数,对my()返回的引用进行操作,并接受参数'>'和'foobar'。此外,这是一个受祝福的typeglob对象(意味着它不能在对象内部保存状态)。
关于我的perlmonks post IO::File vs CORE::的更多信息,请在这里打开:http://www.perlmonks.org/?node_id=763565
另一个C-ism。
int i= 0;
while( i != 12 ) {
/* Some comment
i += 1;
/* Another comment */
}
为什么不行?棉绒会告诉你。然而,C编译器通常会轻松地忽略这一点。我也是。
当我发现问题出在哪里的时候,那真是太棒了。