Python的switch…case语法

Python没有switch…case的语法,不过可以用Dictionary和lambda匿名函数的特性来写出同样优雅的代码,比如这段javascript代码:

switch(value){
    case 1:
        func1();
        break;
    case 2:
        func2();
        break;
    case 3:
        func3();
        break;
}

等价的Python代码:

{
  1: lambda: func1,
  2: lambda: func2,
  3: lambda: func3
}[value]()

Read the rest of this entry >>

PHP5的OOP中的魔术方法

翻阅一些计算机中的旧的文档时,在看What’s New in PHP5时看到的,发现一些PHP5的OOP的一些很有趣的特性,主要是以下方法:

  • __toString()
  • __call()
  • __get()
  • __set()

就当是做一个记录吧

Read the rest of this entry >>