本文介绍了建立一个函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! Hiho, 有一个字符串:dothat 和一个元组:(x,y) 1.构建函数调用的最佳方法是什么:dothat(x,y)? 假设dothat在同一个模块中被定义, 2.是:eval(" dothat(x,y)",None,((''x'',100),(''y'',200))) 执行它的正确方法是什么? 如果在另一个模块中确定了dothat: 3.什么是正确的初始化全局变量以传递给eval的方法? TIA, Francois 解决方案 不是最好的(根本不是)但只有一种方式: def dothat(x,y): print" Called with:",x,y c =(1,2) locals()。get(" dothat")(* c) ---- 被叫:1 2 HtH,Roland 还是很有意思,thx。 def dothat(x,y): print" Called with:",x,y c =(1,2) locals()。get(" dothat")(* c) ---- 被叫搭配:1 2 HtH,Roland Hiho,Having a string: "dothat"and a tuple: (x, y)1. What''s the best way to build a function call like: dothat(x,y)?Assuming dothat is def''d in the same module,2. is: eval("dothat(x,y)", None, ((''x'', 100), (''y'', 200)))the right way to have it executed?If dothat is def''d in another module:3. what would be the right way to initialize the globals to pass to eval ?TIA,Francois 解决方案Not the best (not at all) but one way:def dothat(x,y):print "Called with:", x, yc = (1,2)locals().get("dothat")(*c)----Called with: 1 2HtH, RolandAs you say, not the best, but in fact not really advisable under anycircumstances. locals() returns "module level" stuff only when executed_at_ module level (i.e. not inside a function). Otherwise it willreturn only the local variables of the frame its in.Better is this, since it works for the OP''s requirements in eithersituation.globals()[''dothat''](*c)-PeterStill pretty interesting, thx.def dothat(x,y): print "Called with:", x, yc = (1,2)locals().get("dothat")(*c)----Called with: 1 2HtH, Roland 这篇关于建立一个函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 12:28