如何在 Python 中使用变量作为函数名 [英] How to use a variable as function name in Python

查看:288
本文介绍了如何在 Python 中使用变量作为函数名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中可以使用变量作为函数名吗?例如:

list = [一、二、三]对于列表中的项目:定义项():一些东西()

解决方案

诀窍是使用 globals():

globals()['use_variable_as_function_name']()

将等价于

use_variable_as_function_name()

位于:George Sakkis https://bytes.com/topic/python/answers/792283-calling-variable-function-name

<小时>

以下是我现在需要的上述问题的有用应用(这就是我来到这里的原因):根据 URL 的性质对它们应用特殊功能:

l = ['condition1', 'condition2', 'condition3']

我曾经写过

if 'condition1.'在href:返回 do_something_condition1()如果'条件2.'在href:返回 do_something_condition2()如果'条件3.'在href:返回 do_something_condition3()

等等 - 我的名单现在有 19 名成员,并且还在不断增长.

在研究主题和开发过程中,函数代码已经很自然地成为主函数的一部分,阅读起来很快就很糟糕,因此将工作代码重新定位到函数中已经是一种极大的解脱.

上面这段笨拙的代码可以替换为:

for e in l: # 这是我的条件列表如果 e + '.'in href: # 这是选择正确函数的机制return globals()['do_something_' + e]()

这样无论条件列表可能增长多长时间,主要代码都保持简单易读.

那些与条件标签相对应的函数必须按照惯例声明,当然,这取决于所讨论的 URL 类型的性质:

def do_something_condition1(href):# 特殊代码 1打印('========1========+href)def do_something_condition2(href):# 特殊代码 2打印('========2========+href)def do_something_condition3(href):# 特殊代码 3打印('========3========+href)

测试:

<预><代码>>>>href = 'https://google.com'>>>对于 l 中的 e:... globals()['do_something_' + e](href)...========1========https://google.com========2========https://google.com========3========https://google.com

或者,将其建模为更接近上述场景:

success = '____处理成功____________ 'def do_something_google(href):# 特殊代码 1打印('========我们做谷歌特定的东西========)返回成功+hrefdef do_something_bing(href):# 特殊代码 2打印('========我们做特定于bing的东西========)返回成功+hrefdef do_something_wikipedia(href):# 特殊代码 3打印('========我们做维基百科特定的东西========)返回成功+href

测试:

l = ['google', 'bing', 'wikipedia']href = 'https://google.com'定义测试(参考):对于 l 中的 e:如果 e + '.'在href:return globals()['do_something_' + e](href)>>>测试(参考)========我们做谷歌特定的东西========'________处理成功__________ https://google.com'

结果:

现在对这个问题的进一步细化相当于将条件列表一一扩充,并根据参数编写相应的函数.以上机制将在此后选择正确的.

Would it be possible to use a variable as a function name in python? For example:

list = [one, two, three]
for item in list:
    def item():
         some_stuff()

解决方案

The trick is to use globals():

globals()['use_variable_as_function_name']()

will be equivalent to

use_variable_as_function_name()

found at: George Sakkis https://bytes.com/topic/python/answers/792283-calling-variable-function-name


The following is a useful application of the above questioning I needed right now (that's why I came here): apply special functions to URLs depending on their nature:

l = ['condition1', 'condition2', 'condition3']

I used to write

if 'condition1.' in href:
    return do_something_condition1()
if 'condition2.' in href:
    return do_something_condition2()
if 'condition3.' in href:
    return do_something_condition3()

and so on - my list has 19 members by now and keeps growing.

While investigating the subject and developing, the function code had been quite naturally part of the main function making it soon horrible to read, so relocating the working code into functions was a great relief already.

This clumsy code above can be substituted by:

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]()

This way the main code stays simple and legible no matter how long the list of conditions may grow.

Those functions corresponding to the condition labels have to be declared conventionally, of course, depending on the nature of the type of the URL in question:

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)

Test:

>>> 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

Or, to model it closer to the above scenario:

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 

Test:

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'

Result:

Further elaboration on the problem now just amounts to augment the condition list one by one and write the corresponding functions depending on the argument. The above mechanism will pick the right one thereafter.

这篇关于如何在 Python 中使用变量作为函数名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆