Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #10920 > unrolled thread
| Started by | gervaz <gervaz@gmail.com> |
|---|---|
| First post | 2011-08-05 11:52 -0700 |
| Last post | 2011-08-05 22:05 +0000 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
Get the name of a function gervaz <gervaz@gmail.com> - 2011-08-05 11:52 -0700
Re: Get the name of a function Emile van Sebille <emile@fenx.com> - 2011-08-05 13:19 -0700
Re: Get the name of a function Chris Rebert <clp2@rebertia.com> - 2011-08-05 14:19 -0700
Re: Get the name of a function Grant Edwards <invalid@invalid.invalid> - 2011-08-05 22:05 +0000
| From | gervaz <gervaz@gmail.com> |
|---|---|
| Date | 2011-08-05 11:52 -0700 |
| Subject | Get the name of a function |
| Message-ID | <8295aba8-3eab-4c6e-b3b7-c3421d829220@f7g2000vba.googlegroups.com> |
Hi all, is there a way to retrive the function name like with self.__class__.__name__? Using self.__dict__.__name__ I've got >>> def test(): ... print(self.__dict__.__name__) ... >>> test <function test at 0x0178DDF8> But I really just want the function name, so 'test' Any help? Thanks, Mattia
[toc] | [next] | [standalone]
| From | Emile van Sebille <emile@fenx.com> |
|---|---|
| Date | 2011-08-05 13:19 -0700 |
| Message-ID | <mailman.1940.1312575911.1164.python-list@python.org> |
| In reply to | #10920 |
On 8/5/2011 11:52 AM gervaz said... > Hi all, is there a way to retrive the function name like with > self.__class__.__name__? yes, but not reliably: Python 2.6.4rc2 (r264rc2:75497, Oct 20 2009, 02:55:11) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> def test():pass ... >>> test.__name__ 'test' >>> b=test >>> b.__name__ 'test' >>> Emile
[toc] | [prev] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2011-08-05 14:19 -0700 |
| Message-ID | <mailman.1947.1312579168.1164.python-list@python.org> |
| In reply to | #10920 |
On Fri, Aug 5, 2011 at 11:52 AM, gervaz <gervaz@gmail.com> wrote:
> Hi all, is there a way to retrive the function name like with
> self.__class__.__name__?
>
> Using self.__dict__.__name__ I've got
>
>>>> def test():
> ... print(self.__dict__.__name__)
> ...
Er, where did `self` magically come from?
>>>> test
> <function test at 0x0178DDF8>
>
> But I really just want the function name, so 'test'
>
> Any help?
Courtesy of the "Observations on the three pillars of Python execution" thread:
from inspect import currentframe
def foobar():
my_name = currentframe().f_code.co_name
print(my_name)
I would recommend against a function knowing its own name though,
unless it's for debugging or necessary metaprogramming purposes.
Cheers,
Chris
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2011-08-05 22:05 +0000 |
| Message-ID | <j1hpfp$p11$1@reader1.panix.com> |
| In reply to | #10920 |
On 2011-08-05, gervaz <gervaz@gmail.com> wrote:
> Hi all, is there a way to retrive the function name like with
> self.__class__.__name__?
Not really. There may not be any such thing as "the function name". A
function may have zero names, it may have a dozen names. It may have
names but only in namespaces that aren't accessible.
This question comes up at least once a month, so look back through the
group for threads about finding an object's name, finding a
parameter's name, etc.
Here's a nice article on introspection, but what it talks about as a
"function name" probably isn't what you're interested in:
http://www.ibm.com/developerworks/library/l-pyint/index.html
Here's a stack-overflow question similar to yours:
http://stackoverflow.com/questions/1538342/how-can-i-get-the-name-of-an-object-in-python
--
Grant Edwards grant.b.edwards Yow! But they went to MARS
at around 1953!!
gmail.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web