[问题] 可以用循环呼叫function吗?

楼主: phoenixcx (曼秀雷敦)   2019-05-03 10:58:05
def tmp_sum(tag,cname):
return tag+cname
def tmp_mod(ttag,su):
return ttag*su
a=['tmp_sum','tmp_mod']
for x in a:
i=x(4,3)
有办法将要呼叫的function由list取出放入x再呼叫他吗?
作者: jiyu520 (不要鲫鱼我)   2019-05-03 11:03:00
可以可以查一下 工厂模式或google >> call functions from list
楼主: phoenixcx (曼秀雷敦)   2019-05-03 11:07:00
感谢大大 马上学习一下
作者: jiyu520 (不要鲫鱼我)   2019-05-03 11:21:00
也可查一下python function call by string
楼主: phoenixcx (曼秀雷敦)   2019-05-03 11:44:00
有了 用getattr大概最简单了
作者: TitanEric (泰坦)   2019-05-03 13:10:00
存在dict也很方便唷
作者: adrianshum (Alien)   2019-05-03 15:15:00
如果没必要放string, 直接把 method 的symbol 放里面就好了I.e. arr=[func_1, func_2]
作者: alan23273850   2019-05-06 14:14:00

Links booklink

Contact Us: admin [ a t ] ucptt.com