python getattr调用自己模块,如何在导入的python模块中调用每个函数

I have a program that I wrote that is creating and maintaining an array and I have another module that I wrote that has functions to manipulate the array. Is it possible to call every function in the imported module without having to hard code every function call? Meaning something like this:

#Some way I don't know of to get a list of function objects

listOfFunctions = module.getAllFunctions()

for function in listOfFunctions:

array.function()

I want to do this so I don't have to update my main file every time I add a function to my manipulation module.

I found these:

and also only found listing the functions in a module on the python docs.

I can think of a way to do this using some string manipulation and the eval() function but I feel like there has to be a better, more pythonic way

解决方案

I think you want to do something like this:

import inspect

listOfFunctions = [func_name for func_name, func in module.__dict__.iteritems()\

if inspect.isfunction(func)]

for func_name in listOfFunctions:

array_func = getattr(array, func_name)

array_func()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值