welcom ! Handel home

2016年5月8日 星期日

python Functin set array call Example

# Function Set call by index    
def aa():
  print ("is aa run !\n")
 
def bb():
  print ("is bb run !\n")  

def cc():
  print ("is cc run !\n")

def dd():
  print ("is dd run !\n")

workp = {0:aa,1:bb,2:cc,3:dd}  

if __name__ == '__main__':
  print ("hello dir call test ! \n ================== \n")
  aa(),bb(),cc(),dd()
  print ("hello set call test ! \n ================== \n")
  workp[0]()                                              
  workp[1]()
  workp[2]()
  workp[3]()


run Ex:
hello dir call test !
 ==================
is aa run !
is bb run !
is cc run !
is dd run !
hello set call test !
 ==================
is aa run !
is bb run !
is cc run !
is dd run !

================================================
EXample 2
def plus_1(x):
    return x + 1

def minus_1(x):
    return x - 1

func_map = {'+' : plus_1, '-' : minus_1}

print (func_map['+'](3))  # returns plus_1(3) ==> 4
print (func_map['-'](3))  # returns minus_1(3) ==> 2


沒有留言: