python 的__FILE__,__LINE__功能实现

在C语言里,__FILE__和__LINE__给调试提供了很大的方便,今晚在写PYTHON的时候想到,PYTHON是否有类似的功能实现呢?

GOOGLE一番发现两个方法,试验一下下面这句:
print "here is :",__file__,sys._getframe().f_lineno 

它和C中的两个宏的功能一样了!
但根据找个网页http://nedbatchelder.com/blog/200410.html#e20041003T074926
sys._getframe().f_lineno似乎不是很受推荐的方法。
以下找个网页提供了使用inpect模块的另一种实现发法
http://www.velocityreviews.com/forums/t363977-line-and-file-functionality-in-python.html

import inspect
try :
    c=inspect.currentframe()
    print  c.f_code.co_filename,c.f_lineno,
finally :
    del c

原文地址:https://www.cnblogs.com/xiaoleiel/p/8301219.html