python getattr调用自己模块_如何与getattr()动态地使用lambda:在python中运行python模块?...

从环境(它是一个名为nuke的图形程序)被赋予一个全局对象,我可以在其中添加菜单项并将其与函数连接起来。这个全局对象是这样工作的:

menu.addCommand("Do This!", lambda: DoThings())我想动态读取python模块,并将模块函数添加为全局菜单对象中的项目。我写了一个课程来做到这一点。我的课程的简化版本:

class mymenu():

.

.

.

def _builMenuFromPath(self, basepath, module):

items = []

# ...

# there is code to build the items list. this is a list of the python filenames

# ...

if len(items) > 0:

for item in items:

try:

f = getattr(__import__(module + "." + item), item) # item is the python filename of an module in the folder with the python files and module is the folder

menu.addCommand(item, lambda: f.call()) # f.call() is a function in the dynamic loaded python file

except Exception, e:

pass动态导入效果很好。但是每个生成的菜单项都链接到上次导入的函数。所以每一个菜单项都是一样的。

我不是一个专业的程序员。所以我认为我犯了一个简单的错误。

谢谢你的帮助。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值