在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在Python中:
abs((10+5j)-(25+-5j))
返回~18.03,根据毕达哥拉斯定理,这是点(10,5)和(25,5)之间的距离。之所以会出现这种情况,是因为Python的原生语言支持2+2j形式的复数。由于复数的绝对值形式为a+bj =根号下(a^2+b^2),我们得到距离,同时从一个复数减去另一个复数,然后应用abs(绝对)函数在它上面。
其他回答
c++模板可以用来做一些奇怪的事情,最好的例子是“多维模拟字面量”,它使用模板来计算“绘制”形状的面积。下面的代码对于3x3矩形是有效的c++代码
#include"analogliterals.hpp"
using namespace analog_literals::symbols;
unsigned int c = ( o-----o
| !
! !
! !
o-----o ).area;
再举一个3D立方体的例子:
assert( ( o-------------o
|L \
| L \
| L \
| o-------------o
| ! !
! ! !
o | !
L | !
L | !
L| !
o-------------o ).volume == ( o-------------o
| !
! !
! !
o-------------o ).area * int(I-------------I) );
Python for循环中的else。
来自Python文档:
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
# loop fell through without finding a factor
print n, 'is a prime number'
输出:
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
在PHP中:
<?php
$o = new stdClass;
echo count($o);
?>
..打印1。一直不知道为什么。
PHP
PHP对实例变量和方法的重载处理不一致。考虑:
class Foo
{
private $var = 'avalue';
private function doStuff()
{
return "Stuff";
}
public function __get($var)
{
return $this->$var;
}
public function __call($func, array $args = array())
{
return call_user_func_array(array($this, $func), $args);
}
}
$foo = new Foo;
var_dump($foo->var);
var_dump($foo->doStuff());
转储$var是有效的。即使$var是私有的,__get()也会被任何不存在或不可访问的成员调用,并返回正确的值。这不是doStuff()的情况,它失败于:
Fatal error: Call to private method Foo::doStuff() from context ”.”
我认为其中很多都是在c风格的语言中工作的,但我不确定。
Pass a here document as a function argument: function foo($message) { echo $message . "\n"; } foo(<<<EOF Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc blandit sem eleifend libero rhoncus iaculis. Nullam eget nisi at purus vestibulum tristique eu sit amet lorem. EOF ); You can assign a variable in an argument list. foo($message = "Hello"); echo $message; This works because an assignment is an expression which returns the assigned value. It’s the cause of one of the most common C-style bugs, performing an assignment instead of a comparison.
Python
在Python中,可变的默认函数参数会导致意想不到的结果:
def append(thing, collection=[]):
collection.append(thing)
return collection
print append("foo")
# -> ['foo']
print append("bar")
# -> ['foo', 'bar']
print append("baz", [])
# -> ['baz']
print append("quux")
# -> ['foo', 'bar', 'quux']
空列表是在函数定义时初始化的,而不是在调用时初始化的,因此对它的任何更改都会在函数调用之间保持不变。
MySQL的大小写敏感性
MySQL有非常不寻常的区分大小写的规则:表区分大小写,列名和字符串值不区分大小写:
mysql> CREATE TEMPORARY TABLE Foo (name varchar(128) NOT NULL);
DESCRIBE foo;
ERROR 1146 (42S02): Table 'foo' doesn't exist
mysql> DESCRIBE Foo;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(128) | NO | | NULL | |
+-------+--------------+------+-----+---------+-------+
1 row in set (0.06 sec)
mysql> INSERT INTO Foo (`name`) VALUES ('bar'), ('baz');
Query OK, 2 row affected (0.05 sec)
mysql> SELECT * FROM Foo WHERE name = 'BAR';
+------+
| name |
+------+
| bar |
+------+
1 row in set (0.12 sec)
mysql> SELECT * FROM Foo WHERE name = 'bAr';
+------+
| name |
+------+
| bar |
+------+
1 row in set (0.05 sec)
学习PowerShell时发现:
试着猜一下结果数组是什么样的:
$a = 1, 2
$b = 1, 2+3
$c = 1, 2*3
答案:
1, 2
1, 2, 3
1, 2, 1, 2, 1, 2
哎哟!它动摇了我对PowerShell及其开发人员的信心。