温馨提示×

python如何获取函数名

九三
384
2021-02-23 19:07:47
栏目: 编程语言
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

python如何获取函数名

在python中获取函数名的方法

1.在类内部获取函数名

import sys

class testsqawd(object):

def hello(self):

print('the name of method is ## {}##'.format(sys._getframe().f_code.co_name))

print('the name of class is ## {} ##'.format(self.__class__.__name__))

if __name__ == '__main__':

ttt = testsqawd()

ttt.hello()

2.在函数内部获取函数名

def my_name():

print(sys._getframe().f_code.co_name)

print(inspect.stack()[0][3])

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:c++如何获取函数名

0