本文由我司收集整编,推荐下载,如有疑问,请与我司联系 注释 Python 函数调用带有源代码的字符串 注释 Python 函数调用带有源代码的字符串[英]Comment Python function calls in a string with source code I’m trying to comment function calls from a Python source code. 我正在尝试从 Python 源代码中注释函数调用。 I read the source code in a string, and then I’m trying to apply someu can see, I’m commenting functions in the first indentation level of the source(code outside __name__ == ‘__main__’, methods, and classes) 如您所见,我正在对源的第一个缩进级别中的函数进行注释(__name__ main__’之外的代码,方法和类) But, how can I make this regex work with multiple line calls? 但是,如何使这个正则表达式使用多行调用? Example, if I have the following code into string: 例如,如果我将以下代码写入字符串: Builder.load_string(‘‘‘ type: ‘example’ callback: my_callback() How can I comment each line of this call? 我该如何评论此电话的每一行? 2 This will give you the line numbers you need to comment: 这将为您提供评论所需的行号: mod = “test1”mod = importlib.import_module(mod)p = ==‘__ ast.parse(inspect.getsource(mod))它将输出 16 和 18 两个调用。 It is just a matter of ignoring those lines and writing the new source or doing whatever you want with the updates content: 这只是忽略这些行并编写新源或使用更新内容做任何你想做的事情: import inspectimport importlibimport astdef get_call_lines(mod): mod = importlib.import_module(mod) p = ast.parse(inspect.getsource(mod)) for n in p.body: if 本文来源:https://www.wddqw.com/doc/907cb496b80d6c85ec3a87c24028915f814d8444.html