Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attribute': 0.05; 'globals': 0.09; 'lookup': 0.09; 'python:': 0.09; 'subject:while': 0.09; 'def': 0.10; 'function?': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'wrote:': 0.17; 'define': 0.20; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'run': 0.28; 'dictionary': 0.29; 'prints': 0.29; 'objects': 0.29; 'class': 0.29; 'thursday,': 0.30; 'function': 0.30; 'december': 0.32; 'could': 0.32; 'defining': 0.33; 'to:addr :python-list': 0.33; 'something': 0.35; 'explain': 0.36; 'skip:p 20': 0.36; 'does': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40 Date: Thu, 27 Dec 2012 04:07:18 -0500 From: Mitya Sirenef User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Finding the name of a function while defining it References: <0kknd8tbg7knqa1ng6igbj8u82mqb720oi@4ax.com> <1b2e7f59-6edd-40dd-9d60-35265d67a42d@googlegroups.com> In-Reply-To: <1b2e7f59-6edd-40dd-9d60-35265d67a42d@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356599241 news.xs4all.nl 6980 [2001:888:2000:d::a6]:42084 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35605 On 12/27/2012 03:26 AM, Abhas Bhattacharya wrote: > On Thursday, 27 December 2012 13:33:34 UTC+5:30, Mitya Sirenef wrote: >> How about defining a function that prints value and then calls a function? >> >> >> >> def call(func_name): >> >> print(mydict[func_name]) >> >> globals()[func_name]() >> >> >> >> >> >> You could also define a custom class that does the same thing on attribute >> >> lookup and do something like Call.func_name() . >> >> >> >> -m >> >> >> >> -- >> >> Lark's Tongue Guide to Python: http://lightbird.net/larks/ > Can you explain me what this means? > globals()[func_name]() globals() is a globals dictionary that maps function names to function objects (along with other things), so we get the function object by name and then run it. -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/