python一个函数调用另一个函数的参数,Python的 - 传递一个函数作为参数传递给另一个函数...

I have two files;

The first file, myProgram.py, is a python file with a number of functions

one of the functions contained inside is myFunction

this function would be called with

import myProgram

myProgram.thisFunction()

myProgram.thatFunction()

The second file contains a menu system, with calls to the functions in myProgram.py

I'd like to call a particular function based on an argument passed to a function in file2

def file2_function(function):

myProgram.function

file2_function(thisFunction(x,y,z))

which would essentially create myProgram.thisfunction(x,y,z) and execute it.

I guess I could do this using a bunch of if/elif statements, say:

def file2_function(function):

if function == 1:

myProgram.thisFunction

elif function == 2:

myProgram.thatFunction

Though that could get messy as I'm already using a lot of if/elif/else statements for the menu system.

Is there a better way (or is the if/elif route the way to go?)

解决方案

Alternatively you have getattr. See following snippet:

>>> import sys

>>> getattr(sys,'exc_info')

>>> getattr(sys,'exc_info')()

(None, None, None)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值