是否可以在python中使用变量作为函数名?例如:
list = [one, two, three]
for item in list:
def item():
some_stuff()
Run Code Online (Sandbox Code Playgroud)
您不能使用变量定义函数,但可以将函数重新绑定到变量名称.下面是将它们添加到模块的全局命名空间的示例.
one = 'one'
two = 'two'
three = 'three'
l = [one, two, three]
def some_stuff():
print("i am sure some stuff")
for item in l:
def _f():
some_stuff()
globals()[item] = _f
del _f
one()
two()
three()
Run Code Online (Sandbox Code Playgroud)
Python中的函数是具有引用它们的名称的对象,因此您可以传递它们,存储在列表和字典中(创建跳转表时常用).
即这是有效的:
def one():
print "1"
def two():
print "2"
def three():
print "3"
l = [one, two, three]
for item in l:
item()
Run Code Online (Sandbox Code Playgroud)
将打印:
1
2
3
Run Code Online (Sandbox Code Playgroud)
不要使用list
变量名,因为这样你重新定义了buildin.
def
与编译语言中的函数defenition不同,它也是执行的语句.所以,当你打电话def item():
你不用于定义功能one
,two
,three
,而是重新定义item
名称.
一般来说,你想要做什么并不是很清楚,但它看起来不是一个好主意.可以解释你想要完成什么,或者重新思考你想要的方式.
诀窍是使用globals():
globals()['use_variable_as_function_name']()
Run Code Online (Sandbox Code Playgroud)
相当于
use_variable_as_function_name()
Run Code Online (Sandbox Code Playgroud)
可在以下网址找到:George Sakkis https://bytes.com/topic/python/answers/792283-calling-variable-function-name
以下是我现在需要的上述询问的有用应用(这就是为什么我来这里):根据URL的性质将特殊功能应用于URL:
l = ['condition1', 'condition2', 'condition3']
Run Code Online (Sandbox Code Playgroud)
我曾经写
if 'condition1.' in href:
return do_something_condition1()
if 'condition2.' in href:
return do_something_condition2()
if 'condition3.' in href:
return do_something_condition3()
Run Code Online (Sandbox Code Playgroud)
依此类推-我的名单目前有19个成员,并且还在不断增长。
在研究主题并进行开发时,功能代码已经很自然地成为主要功能的一部分,使其很快就难以阅读,因此将工作代码重新定位到功能中已经是很大的缓解。
上面的笨拙代码可以替换为:
for e in l: # this is my condition list
if e + '.' in href: # this is the mechanism to choose the right function
return globals()['do_something_' + e]()
Run Code Online (Sandbox Code Playgroud)
这样,无论条件列表增加多长时间,主代码都将保持简单易读。
当然,取决于条件的URL类型的性质,必须常规地声明与条件标签相对应的那些函数:
def do_something_condition1(href):
# special code 1
print('========1=======' + href)
def do_something_condition2(href):
# special code 2
print('========2=======' + href)
def do_something_condition3(href):
# special code 3
print('========3=======' + href)
Run Code Online (Sandbox Code Playgroud)
测试:
>>> href = 'https://google.com'
>>> for e in l:
... globals()['do_something_' + e](href)
...
========1=======https://google.com
========2=======https://google.com
========3=======https://google.com
Run Code Online (Sandbox Code Playgroud)
或者,使其更接近于上述场景:
success = '________processed successfully___________ '
def do_something_google(href):
# special code 1
print('========we do google-specific stuff=======')
return success + href
def do_something_bing(href):
# special code 2
print('========we do bing-specific stuff=======')
return success + href
def do_something_wikipedia(href):
# special code 3
print('========we do wikipedia-specific stuff=======')
return success + href
Run Code Online (Sandbox Code Playgroud)
测试:
l = ['google', 'bing', 'wikipedia']
href = 'https://google.com'
def test(href):
for e in l:
if e + '.' in href:
return globals()['do_something_' + e](href)
>>> test(href)
========we do google-specific stuff=======
'________processed successfully___________ https://google.com'
Run Code Online (Sandbox Code Playgroud)
结果:
现在,对该问题的进一步详细说明就等于一个一个地增加条件列表,并根据自变量编写相应的函数。之后,上述机制将选择合适的机制。
这是包装在类中的解决方法。它使用字典进行映射:
class function_class:
def __init__(self,fCase):
fDic = {'A':self.A, # mapping: string --> variable = function name
'B':self.B,
'C':self.C}
self.fActive = fDic[fCase]
def A(self): print('here runs function A')
def B(self): print('here runs function B')
def C(self): print('here runs function C')
def run_function(self):
self.fActive()
#---- main ----------
fList = ['A','B','C'] # list with the function names as strings
for f in fList: # run through the list
g = function_class(f)
g.run_function()
Run Code Online (Sandbox Code Playgroud)
输出为:
here runs function A
here runs function B
here runs function C
Run Code Online (Sandbox Code Playgroud)
小智 1
最简洁的答案是不。当您声明变量时,您已将名称绑定到对象。声明函数时也是如此。您可以在 python 控制台中亲自尝试一下,看看会发生什么:
>name=1
>name
1
>def name(x): print(x+1)
>name
function name at 0x000001CE8B8122F0
Run Code Online (Sandbox Code Playgroud)