Files
jlengrand.github.io/_posts/2011-12-28-simply-print-current-function-name.markdown
Julien Lengrand-Lambert c9cc716349 back to HTTP
2018-03-17 11:41:01 +01:00

1.2 KiB

layout, status, published, title, author, author_login, author_email, author_url, wordpress_id, wordpress_url, date, categories, tags, comments
layout status published title author author_login author_email author_url wordpress_id wordpress_url date categories tags comments
post publish true Simply print current function name Julien Lengrand-Lambert jlengrand julien@lengrand.fr http://www.lengrand.fr 476 http://www.lengrand.fr/?p=476 2011-12-28 16:55:21.000000000 +01:00
Python
function name
print
debug

Hi all,

When developing, I hate having to search in which portion of my code I am. For Tippy, I searched for a way to always display the function name in case of an error.

Hopefully, Python offers a simple (but curious) way to perform this.

Here is how to print your function name as a string in Python :

{% highlight python %} import sys

def tutut(): """ Dum function displaying its name! """ print sys._getframe().f_code.co_name

if name == 'main': tutut() {% endhighlight %}

And here is the result

{% highlight bash %} [airballman@ubuntu:~]$ python tutut.py tutut {% endhighlight %}

There it is !

You can also find this tip in my Programming Tips page, in the Python section.