在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在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
其他回答
INTERCAL可能是最奇怪的语言特征的最佳汇编。我个人最喜欢的是COMEFROM语句,它(几乎)与GOTO相反。
COMEFROM is roughly the opposite of GOTO in that it can take the execution state from any arbitrary point in code to a COMEFROM statement. The point in code where the state transfer happens is usually given as a parameter to COMEFROM. Whether the transfer happens before or after the instruction at the specified transfer point depends on the language used. Depending on the language used, multiple COMEFROMs referencing the same departure point may be invalid, be non-deterministic, be executed in some sort of defined priority, or even induce parallel or otherwise concurrent execution as seen in Threaded Intercal. A simple example of a "COMEFROM x" statement is a label x (which does not need to be physically located anywhere near its corresponding COMEFROM) that acts as a "trap door". When code execution reaches the label, control gets passed to the statement following the COMEFROM. The effect of this is primarily to make debugging (and understanding the control flow of the program) extremely difficult, since there is no indication near the label that control will mysteriously jump to another point of the program.
在SQL
NULL不等于NULL
所以你不能:
WHERE myValue == NULL
这将总是返回false。
NULL != NULL
C和c++中的三联体。
int main() {
printf("LOL??!");
}
这将打印LOL|,因为trigraph ??!转换为|。
我不敢相信这里还没有这个:JSF属性访问。
在JSF UI标签中,你可以将服务器端对象的属性值引用到接口中:
<h:inputText value="#{myObject.property}></h:inputText>
问题是Java不支持属性,所以你必须编写以get和set开头的方法,以便将UI对象链接到服务器上的“属性”。
public void setProperty(String property){...}
public String getProperty(){...}
当我第一次学习JSF时,这让我感到困惑,我仍然认为它值得使用wtf。尽管在Java实现对c#风格属性的支持之前,确实没有其他方法可以做到这一点。
在C或c++中,sizeof…参数的圆括号是可选的。假设参数不是类型:
void foo() {
int int_inst;
// usual style - brackets ...
size_t a = sizeof(int);
size_t b = sizeof(int_inst);
size_t c = sizeof(99);
// but ...
size_t d = sizeof int_inst; // this is ok
// size_t e = sizeof int; // this is NOT ok
size_t f = sizeof 99; // this is also ok
}
我一直不明白这是为什么!