Python 我能知道从哪里调用了函数吗?
我开始用Python 3编程。我想知道是否有任何函数或方法可以让我知道函数是从哪个位置(可能是从哪个行)调用的 例如,如果我有以下代码Python 我能知道从哪里调用了函数吗?,python,python-3.x,Python,Python 3.x,我开始用Python 3编程。我想知道是否有任何函数或方法可以让我知道函数是从哪个位置(可能是从哪个行)调用的 例如,如果我有以下代码 1 def foo(): 2 print("Hi!") 3 4 def suma(a, b): 5 return a+b 6 7 8 def main(): 9 foo() 10 suma(3,6) 11 12 13 if __name__ == "__main__": 14
1 def foo():
2 print("Hi!")
3
4 def suma(a, b):
5 return a+b
6
7
8 def main():
9 foo()
10 suma(3,6)
11
12
13 if __name__ == "__main__":
14 main()
15 else:
16 print("Function main() not exist")
17
不知何故,我们知道这个函数:
foo
:它在第1行中定义。
它已从第9行的main
调用
suma
:它在第4行中定义。
它已从第10行的main
调用
有没有这样的功能或者类似的功能
可能是这样的:
foo.__ code __. co_nlocals
suma.__ code __. co_nlocals
但是有了前面提到的。检查
模块,您就可以实现这一点(特别是.getsourcelines()
方法)
def foo():
打印(“嗨!”)
def suma(a、b):
返回a+b
进口检验
打印(检查.getsourcelines(foo))
打印(检查.getsourcelines(suma))
(['def foo():\n',print(“Hi!”)\n'],1)
(['defsuma(a,b):\n','returna+b\n'],4)
您将看到结果是一个元组,其中第一个元素是函数的源,第二个元素是定义函数的行
如果您需要,也可以使用一种等效的方法来获取该文件。
- 定义函数的行号
使用:
inspect.getsourcelines(函数的名称)[1]
- 从中删除函数的行
用法:
调用.inspect.stack()[1][2]
- 调用/调用函数
使用:
inspect.stack()[1][3]
- (可选)包含它的模块
使用:
函数的名称。\u模块\u
例如。。。(我添加了一个附加函数X)
如果我们列出前面的行,代码如下:
01 import inspect
02
03 def foo(msg):
04 print(msg)
05 ###▼ YOUR INSPECTION CODE ▼###
06 print("\t«{}»\tLine number in which the function is defined.".
07 format(inspect.getsourcelines(foo)[1]))
08 print("\t«{}»\tLine from which the function has been called.".
09 format(inspect.stack()[1][2]))
10 print("\t«{}»\tInvoking/calling function.".format(inspect.stack()[1][3]))
11 print("\t«{}»\tModule in which it is contained.\n".format(foo.__module__))
12
13 def suma(a, b):
14 foo("Call from [suma()], on the line [14]")
15 return a+b
16
17 def difference(a, b):
18 foo("Call from [difference()], on the line [18]")
19 return a-b
20
21 def main():
22 foo("Call from [main()], on the line [22]")
23 suma(3,6)
24 foo("Call from [main()], on the line [24]")
25 difference(5,2)
26
27 if __name__ == "__main__":
28 main()
结果是:
也许-模块可以帮助您详细说明您的实际用例是什么?你为什么要这么做?或者更确切地说,你为什么认为你需要这样做?我这样说的原因是因为你提到你才刚刚开始——初学者经常会问关于他们想象的解决问题的方法的问题,而不是问题本身。如果你分享这个用例,有可能有人会建议一种替代方法。作为旁注,第15行中的“else:”子句毫无意义。如果您调用一个不存在的函数,解释器将非常乐意告诉您,并提供一条有用的错误消息和一个回溯。永远不要检查您不准备处理的错误情况。您可以使用调试器来确定哪个是调用方。例如,如果else
子句错误-main
将存在,无论\uuuuu name\uuuuuu==“\uuuuuuuu main\uuuuu”
。如果uuu name uuu==“uuuu main uuuu”
检查的目的不是这样的话,if\uuuu name\uuuu==“\uuuuu main\uuuuu”
检查我们是否应该运行文件的脚本功能,而不是我们是否可以这样做。inspect.getsourcelines
没有说明函数从何处调用。我很乐意接受关于问题另一半的建议。当我最初发布这篇文章时,另一个人用stack解决了这部分问题。我只是想解决“foo:它在第1行中定义”部分。欢迎来到Stackoverflow。第一个答案很好!
01 import inspect
02
03 def foo(msg):
04 print(msg)
05 ###▼ YOUR INSPECTION CODE ▼###
06 print("\t«{}»\tLine number in which the function is defined.".
07 format(inspect.getsourcelines(foo)[1]))
08 print("\t«{}»\tLine from which the function has been called.".
09 format(inspect.stack()[1][2]))
10 print("\t«{}»\tInvoking/calling function.".format(inspect.stack()[1][3]))
11 print("\t«{}»\tModule in which it is contained.\n".format(foo.__module__))
12
13 def suma(a, b):
14 foo("Call from [suma()], on the line [14]")
15 return a+b
16
17 def difference(a, b):
18 foo("Call from [difference()], on the line [18]")
19 return a-b
20
21 def main():
22 foo("Call from [main()], on the line [22]")
23 suma(3,6)
24 foo("Call from [main()], on the line [24]")
25 difference(5,2)
26
27 if __name__ == "__main__":
28 main()
Call from [main()], on the line [22]
«3» Line number in which the function is defined.
«22» Line from which the function has been called.
«main» Invoking/calling function.
«__main__» Module in which it is contained.
Call from [suma()], on the line [14]
«3» Line number in which the function is defined.
«14» Line from which the function has been called.
«suma» Invoking/calling function.
«__main__» Module in which it is contained.
Call from [main()], on the line [24]
«3» Line number in which the function is defined.
«24» Line from which the function has been called.
«main» Invoking/calling function.
«__main__» Module in which it is contained.
Call from [difference()], on the line [18]
«3» Line number in which the function is defined.
«18» Line from which the function has been called.
«difference» Invoking/calling function.
«__main__» Module in which it is contained.