Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10934

Re: Get the name of a function

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.010
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'help?': 0.03; 'python': 0.08; '>>>>': 0.09; 'am,': 0.12; 'debugging': 0.13; 'def': 0.15; 'subject:function': 0.16; 'test():': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'knowing': 0.23; 'purposes.': 0.23; 'aug': 0.24; 'function': 0.27; 'all,': 0.28; 'import': 0.28; 'message-id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'chris': 0.32; 'there': 0.33; "i've": 0.34; '...': 0.34; 'test': 0.34; 'received:209.85.161': 0.35; 'unless': 0.36; 'fri,': 0.36; 'skip:" 10': 0.36; 'using': 0.37; 'but': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; "it's": 0.40; 'where': 0.40; 'subject:name': 0.67; 'subject:Get': 0.71; "'test'": 0.84; 'sender:addr:chris': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=T0x9GAsYWIjzwYHOCPk/qYj8rq95e7EeDAOYC+oaCSk=; b=BKDsS186LWkQBuVqPJKsbQfWJqknlw/Ma0kAZ1cTrVx+iuGnbufqTetgf6gOh8NtUK MaXYS17TwmX9gUMT23UwN0ogJSwDlQuasraJSxcgfoBu+N2KAhD7pQBdHLOVS2LHPf0L lceFaTEFJJIedTcpbU5QN8DxdmyyFeleI9ptQ=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <8295aba8-3eab-4c6e-b3b7-c3421d829220@f7g2000vba.googlegroups.com>
References <8295aba8-3eab-4c6e-b3b7-c3421d829220@f7g2000vba.googlegroups.com>
Date Fri, 5 Aug 2011 14:19:21 -0700
X-Google-Sender-Auth bWB3-ZAI3iEorL2ro_p69tOzmFU
Subject Re: Get the name of a function
From Chris Rebert <clp2@rebertia.com>
To gervaz <gervaz@gmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1947.1312579168.1164.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1312579169 news.xs4all.nl 23846 [2001:888:2000:d::a6]:49631
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10934

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web