如果应使用列表或变量调用模块,则如何调用Python中模块的函数?

发布于 2025-02-11 06:15:44 字数 361 浏览 1 评论 0原文

我有一个模块,在模块中是一个子组件,其中包含众多函数。我需要迭代包含模块名称的名称列表并调用函数。这行不通,“因为模块没有属性列表”。我如何使列表[x]可callable作为模块的子组件,而不是“列表”名称。

文件1:

def x():
   print('x')

def y():
   print('y')

def z():
   print('z')

文件2:

import module # containing functions x(),y(),z()
list = ['x','y','z']
for x in list:
   module.list[x]()

I have a module, in the module is a subcomponent which contains a multitude of functions. I need to iterate through a list of names which contains the names of the modules and call the functions. this does not work, "because module has no attribute list". How do I make list[x] callable as a subcomponent of module instead of "list" the name.

file 1:

def x():
   print('x')

def y():
   print('y')

def z():
   print('z')

file 2:

import module # containing functions x(),y(),z()
list = ['x','y','z']
for x in list:
   module.list[x]()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾忌 2025-02-18 06:15:44

getaTtr()

import module

lst = ["x", "y", "z"]

for item in lst:
    getattr(module, item)()

尝试

<function x at 0x7f2c56129e50>
<function y at 0x7f2c56129f70>
<function z at 0x7f2c560e4c10>

Try getattr():

import module

lst = ["x", "y", "z"]

for item in lst:
    getattr(module, item)()

Prints:

<function x at 0x7f2c56129e50>
<function y at 0x7f2c56129f70>
<function z at 0x7f2c560e4c10>
可爱暴击 2025-02-18 06:15:44

您可以使用getAttr访问模块的“内容”。

使用 dir dir to to to to in IntoSpect”模块的内容:

import my_module

func_names = ['x','y','z']
callable_funcs = []

for stuffs in dir(my_module):
    if stuff in func_names:
        callable_funcs.append(getattr(my_module, stuff))

# make a dictionary
funcs = dict(zip(func_names, callable_funcs))

# usage
x = funcs['x']
print(x())

使用getAttrhasattr的另一种方法:

import my_module

func_names = ['x','y','z']
callable_funcs = []

for f_name in func_names:
    if hasattr(my_module, f_name):
        callable_funcs.append(getattr(my_module, f_name))

# make a dictionary
funcs = dict(zip(func_names, callable_funcs))

# usage
x = funcs['x']
print(x())

也可以使用字典理解来完成:

import my_module

func_names = ['x','y','z']

funcs = {f_name: getattr(my_module, f_name) for f_name in func_names if hasattr(my_module, f_name)}

You can use getattr to access to the "content" of the module.

Using dir to "introspect" the content of the module:

import my_module

func_names = ['x','y','z']
callable_funcs = []

for stuffs in dir(my_module):
    if stuff in func_names:
        callable_funcs.append(getattr(my_module, stuff))

# make a dictionary
funcs = dict(zip(func_names, callable_funcs))

# usage
x = funcs['x']
print(x())

Another way using getattr and hasattr:

import my_module

func_names = ['x','y','z']
callable_funcs = []

for f_name in func_names:
    if hasattr(my_module, f_name):
        callable_funcs.append(getattr(my_module, f_name))

# make a dictionary
funcs = dict(zip(func_names, callable_funcs))

# usage
x = funcs['x']
print(x())

It can be done also using a dictionary comprehension:

import my_module

func_names = ['x','y','z']

funcs = {f_name: getattr(my_module, f_name) for f_name in func_names if hasattr(my_module, f_name)}
花落人断肠 2025-02-18 06:15:44

您为什么不只是将函数存储在列表中?

>>> def x():
...     print("x")
... 
>>> def y():
...     print("y")
... 
>>> functions = [x, y]
>>> for f in functions:
...     f()
... 
x
y
>>> 

Why don't you just store the functions in the list?

>>> def x():
...     print("x")
... 
>>> def y():
...     print("y")
... 
>>> functions = [x, y]
>>> for f in functions:
...     f()
... 
x
y
>>> 

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文