深度阅读

How to get the name of a function in Python

作者
作者
2023年08月22日
更新时间
6.46 分钟
阅读时间
0
阅读量

To get the name of a function in Python, you can use the __name__ attribute of the function object. Here’s an example:

def my_function():
    pass

print(my_function.__name__)

This will output my_function, which is the name of the function.

Alternatively, you can use the inspect module to get the name of the current function. Here’s an example:

import inspect

def my_function():
    print(inspect.stack()[0][3])

my_function()

This will output my_function, which is the name of the current function. Note that this method can only be used to get the name of the current function, not any arbitrary function.

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。