Python可能没有内置switch语句,但仍然可以模拟它。
switch = {1: func1, 2: func2, 3: func3} # case statements
switch.get(switchvar, default)() # default is used if the cases are not matched
下面是一些有趣的事情,你可以在C做++(偶尔确实有使用):
// default
template <int i> void int_switch()
{
std::cout << i << '\n';
}
// case 1
template<> void int_switch<1>()
{
std::cout << "one\n";
}
// case 2
template<> void int_switch<2>()
{
std::cout << "two\n";
}
const int switch_var = 3;
int_switch<switch_var>(); // Outputs "3\n"
当然,只有当模板参数是一个常量表达式,因此使用的作品const int
而不是只有int
。
来源
2014-01-09 20:38:44
JAB
[goto](http://en.wikipedia.org/wiki/Goto) – celeriko
http://en.wikipedia.org/wiki/LOLCODE – bf2020
“列出具有属性X的事物”的问题并不适合为这个网站。 –