使用变量调用python函数 [英] calling python functions using variables

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

问题描述

大家好!


这是一个(相对)新手问题

我正在用Python写一个shell而我正面临一个问题


问题是,在收到用户的输入后,我必须执行

命令,这是一个python函数


i调用这样的''ls''命令

commands.ls()

其中commands.py是同一目录下的文件

我想做的是

commands.VARIABLE()

其中VARIABLE保存我想要执行的函数的名称

并取决于用户输入的内容

我该怎么做才能实现这一目标?


任何帮助表示赞赏

谢谢


-creo

PS如果你想让我澄清这个问题,请告诉我

Hi all!

this is a (relatively) newbie question
I am writing a shell in Python and I am facing a problem

The problem is, after taking the input from user, i have to execute the
command which is a python function

i invoke an ''ls'' command like this
commands.ls()
where commands.py is a file in the same directory

what i want to do is
commands.VARIABLE()
where VARIABLE holds the name of the function which i want to execute
and depends on what the user has typed
what should i do to achieve that?

any help is appreciated

thank you

-creo
P.S. if you want me to clarify the question, please tell me so

推荐答案

creo写道:
creo wrote:
我调用这样的''ls''命令
commands.ls()
其中commands.py是同一目录下的文件

我想要什么是
commands.VARIABLE()
其中VARIABLE保存我想要执行的函数的名称
并取决于用户键入的内容
i invoke an ''ls'' command like this
commands.ls()
where commands.py is a file in the same directory

what i want to do is
commands.VARIABLE()
where VARIABLE holds the name of the function which i want to execute
and depends on what the user has typed




你想要


getattr(命令,VARIABLE)()


彼得



You want

getattr(commands, VARIABLE)()

Peter


Peter Otten< __ ******* @ web.de>写道:
Peter Otten <__*******@web.de> writes:
creo写道:
我想做的是
commands.VARIABLE()
其中VARIABLE保持名称我想要执行的功能
取决于用户键入的内容
what i want to do is
commands.VARIABLE()
where VARIABLE holds the name of the function which i want to execute
and depends on what the user has typed



你想要

getattr(命令,VARIABLE)()



You want

getattr(commands, VARIABLE)()




你还需要预测值绑定的情况

VARIABLE不是''命令中属性的名称''。


首先处理生成的NameError异常(EAFP [0])或测试

是否存在属性(LBYL [1])。


[0]宽恕比宽恕更容易

[1]在你跳跃之前先看看


-

\我们的产品并不是为安全而设计的。 - Brian |

` \情人节,微软Windows高级副总裁|

Ben Finney


Ben Finney写道:
Ben Finney wrote:
Peter Otten< __ ******* @ web.de>写道:
(snip)
Peter Otten <__*******@web.de> writes: (snip)

你想要
getattr(命令,VARIABLE)()

You want
getattr(commands, VARIABLE)()



你还需要预测
VARIABLE的值不是''命令''中属性的名称的情况。

要么处理结果NameError异常(EAFP [0])



You''ll also need to anticipate the situation where the value bound to
VARIABLE is not the name of an attribute in ''commands''.

Either deal with the resulting NameError exception (EAFP[0])




试试:

getattr(命令,VARIABLE)()
$ b $ name除了NameError:

print>> sys.stderr,Unknown command,VARIABLE

或测试
首先是否存在属性(LBYL [1])。



try:
getattr(commands, VARIABLE)()
except NameError:
print >> sys.stderr, "Unknown command", VARIABLE
or test
first whether the attribute exists (LBYL[1]).




command = getattr(commands,VARIABLE,None)
如果命令为None,则为


print>> sys.stderr,Unknown command,VARIABLE

else:

命令()


我会去的第一个解决方案。


-

bruno desthuilliers

python -c" print''@''。join( [''。''。加入([w [:: - 1] for p in p.split(''。'')])for

p in''o **** @ xiludom.gro''。split(''''')])"



command = getattr(commands, VARIABLE, None)
if command is None:
print >> sys.stderr, "Unknown command", VARIABLE
else:
command()

I''d go for the first solution.

--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


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

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