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: 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 To: gervaz 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Fri, Aug 5, 2011 at 11:52 AM, gervaz 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(): > ... =C2=A0 =C2=A0 print(self.__dict__.__name__) > ... Er, where did `self` magically come from? >>>> test > > > But I really just want the function name, so 'test' > > Any help? Courtesy of the "Observations on the three pillars of Python execution" thr= ead: from inspect import currentframe def foobar(): my_name =3D 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