"Python series" Python Gets the filename, function name, and line number of the current location, __ function

Source: Internet
Author: User
Python gets the name and line number of the function where the current position is located

C + + program debugging often used in several macros: __file__, __function__, __line__, recently written Python program encountered problems debugging also want to use this way, looked for the next online data, found that there are many people ask, estimates are like me just from C /c++ go to Python's bar. The function name and line number in Python where the current position is obtained are encapsulated in SYS and are obtained as follows:

Import sys def get_cur_info (): Print Sys._getframe (). F_code.co_filename #当前文件名, you can get print sys._getframe through __file__ (). F_code.co_name #当前函数名 Print Sys._getframe (). F_lineno #当前行号 Get_cur_info ()

Other functions Please refer to: Dir (Sys._getframe ()).

Turn from: http://www.5dcode.com/?p=551

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.