gpt4 book ai didi

Python - inspect.getmembers 按源代码顺序

转载 作者:太空宇宙 更新时间:2023-11-03 12:43:55 26 4
gpt4 key购买 nike

我正在尝试按照源代码的顺序使用 inspect.getmembers 从模块中获取函数列表。

下面是代码

def get_functions_from_module(app_module):
list_of_functions = dict(inspect.getmembers(app_module,
inspect.isfunction))

return list_of_functions.values

当前代码不会按源代码顺序返回函数对象列表,我想知道是否可以对其进行排序。

谢谢!

最佳答案

我想我想出了一个解决方案。

def get_line_number_of_function(func):
return func.__code__.co_firstlineno

def get_functions_from_module(app_module):
list_of_functions = dict(inspect.getmembers(app_module,
inspect.isfunction))

return sorted(list_of_functions.values(), key=lambda x:
get_line_number_of_function(x))

关于Python - inspect.getmembers 按源代码顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47082391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com