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

52 lines
1.2 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
layout: post
status: publish
published: true
title: ! 'Simply print current function name '
author: Julien Lengrand-Lambert
author_login: jlengrand
author_email: julien@lengrand.fr
author_url: http://www.lengrand.fr
wordpress_id: 476
wordpress_url: http://www.lengrand.fr/?p=476
date: 2011-12-28 16:55:21.000000000 +01:00
categories:
- Python
tags:
- function name
- print
- debug
comments: []
---
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 <strong>print your function name as a string</strong> 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 <strong><a href="http://www.lengrand.fr/programming-tips-2/#python">Python section</a>.</strong>