在python中以字符串形式获取函数名称 [英] Get function name as a string in python

查看:169
本文介绍了在python中以字符串形式获取函数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何在Python中以字符串形式获取函数名称?

Possible Duplicate:
How do I get the name of a function or method from within a Python function or method?
How to get the function name as string in Python?

我有一个名为 func 的函数,我希望能够以字符串的形式获取这些函数的名称.

I have a function named func, I'd like to be able to get the functions name as a string.

伪python:

def func () :
    pass

print name(func)

这将显示'func'.

This would print 'func'.

推荐答案

很简单.

print func.__name__

但是您必须小心:

>>> def func():
...     pass
... 
>>> new_func = func
>>> print func.__name__
func
>>> print new_func.__name__
func

这篇关于在python中以字符串形式获取函数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆