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


Groups > comp.lang.python > #35598

Re: Finding the name of a function while defining it

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <msirenef@lightbird.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'essentially': 0.04; 'explicitly': 0.04; 'attribute': 0.05; 'function,': 0.07; 'python': 0.09; 'lookup': 0.09; 'object)': 0.09; 'python:': 0.09; 'subject:while': 0.09; 'unnamed': 0.09; 'def': 0.10; 'programmer': 0.11; '(the': 0.15; 'called,': 0.16; 'dictionary).': 0.16; 'function?': 0.16; 'hard-code': 0.16; 'lambda': 0.16; 'merely': 0.16; 'naming': 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; 'string': 0.17; 'wrote:': 0.17; 'exists': 0.17; 'creates': 0.18; 'tim': 0.18; '>>>': 0.18; 'code.': 0.20; 'skip:p 30': 0.20; 'define': 0.20; 'names.': 0.22; 'questions:': 0.22; 'defined': 0.22; 'this:': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'skip:" 20': 0.26; 'am,': 0.27; 'compiled': 0.27; 'functions.': 0.27; 'object,': 0.27; "doesn't": 0.28; 'dictionary': 0.29; 'name?': 0.29; 'prints': 0.29; 'class': 0.29; "i'm": 0.29; 'that.': 0.30; 'normally': 0.30; 'thursday,': 0.30; 'function': 0.30; 'expect': 0.31; 'code': 0.31; 'asking': 0.32; 'december': 0.32; 'could': 0.32; 'print': 0.32; 'defining': 0.33; 'like:': 0.33; 'to:addr:python-list': 0.33; 'knowledge': 0.33; 'doing': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'skip:p 20': 0.36; 'bad': 0.37; 'does': 0.37; 'two': 0.37; 'being': 0.37; 'quite': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'more': 0.63; 'dont': 0.64; 'grab': 0.64; 'here': 0.65; 'answer.': 0.71; 'why?': 0.84; 'abc': 0.91
Date Thu, 27 Dec 2012 03:03:34 -0500
From Mitya Sirenef <msirenef@lightbird.net>
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 <c9548d77-ccc3-4b47-b84b-9a9f0c2852ce@googlegroups.com> <0kknd8tbg7knqa1ng6igbj8u82mqb720oi@4ax.com> <c9189a65-74ee-4ea7-a4b6-33ece392ea8f@googlegroups.com>
In-Reply-To <c9189a65-74ee-4ea7-a4b6-33ece392ea8f@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 <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.1333.1356595418.29569.python-list@python.org> (permalink)
Lines 87
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1356595418 news.xs4all.nl 6986 [2001:888:2000:d::a6]:41064
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:35598

Show key headers only | View raw


On 12/27/2012 02:45 AM, Abhas Bhattacharya wrote:
> On Thursday, 27 December 2012 10:22:15 UTC+5:30, Tim Roberts  wrote:
>> Abhas Bhattacharya <abhasbhattacharya2@gmail.com> wrote:
>>
>>> While I am defining a function, how can I access the name (separately as
>>> string as well as object) of the function without explicitly naming
>>> it(hard-coding the name)?
>>> For eg. I am writing like:
>>> def abc():
>>>     #how do i access the function abc here without hard-coding the name?
>>
>>
>> Why?  Of what value would that be?
>>
>>
>>
>> Note that I'm not merely being obstructionist here.  What you're asking
>>
>> here is not something that a Python programmer would normally ask.  The
>>
>> compiled code in a function, for example, exists as an object without a
>>
>> name.  That unnamed object can be bound to one or more function names, but
>>
>> the code doesn't know that.  Example:
>>
>>
>>
>> def one():
>>
>>      print( "Here's one" )
>>
>>
>>
>> two = one
>>
>>
>>
>> That creates one function object, bound to two names.  What name would you
>>
>> expect to grab inside the function?
>>
>>
>>
>> Even more obscure:
>>
>>
>>
>> two = lamba : "one"
>>
>> one = two
>>
>>
>>
>> Which one of these is the "name" of the function?
>>
>> -- 
>>
>> Tim Roberts, timr@probo.com
>>
>> Providenza & Boekelheide, Inc.
> It is of quite value to me.
> Because I have this situation:
> I have used a dictionary with "function_name":value pair in the top of the code. Now when some function is called, I need to print the value assigned to its name in the dictionary (the functions are defined after the dictionary). Now there is only one bad way-around for me: I need to hard-code the name in the function like this:
> def function_name():
>      print(dict_name.get("function_name"))
> but ofcourse it is a bad thing to do because I have a lot of this type of  functions. It would be better if I can can use the same code for all of them, because they are all essentially doing the same thing.
>
> Now, for your questions:
> If i call one() and two() respectively, i would like to see "one" and "two".
> I dont have much knowledge of lambda functions, neither am i going to use them, so that's something I cant answer.

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/

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


Thread

Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-25 18:00 -0800
  Re: Finding the name of a function while defining it Roy Smith <roy@panix.com> - 2012-12-25 22:11 -0500
    Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:26 -0800
      Re: Finding the name of a function while defining it Chris Angelico <rosuav@gmail.com> - 2012-12-27 18:48 +1100
        Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:52 -0800
        Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:52 -0800
    Re: Finding the name of a function while defining it Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-27 12:59 +0000
  Re: Finding the name of a function while defining it Tim Roberts <timr@probo.com> - 2012-12-26 20:52 -0800
    Re: Finding the name of a function while defining it Chris Angelico <rosuav@gmail.com> - 2012-12-27 16:44 +1100
      Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:46 -0800
        Re: Finding the name of a function while defining it Chris Angelico <rosuav@gmail.com> - 2012-12-27 18:52 +1100
          Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:55 -0800
          Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:55 -0800
      Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:46 -0800
        Re: Finding the name of a function while defining it Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-27 10:58 +0000
          Re: Finding the name of a function while defining it Tim Chase <python.list@tim.thechases.com> - 2012-12-27 07:32 -0600
            Re: Finding the name of a function while defining it Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-27 13:34 +0000
          Re: Finding the name of a function while defining it Roy Smith <roy@panix.com> - 2012-12-27 10:09 -0500
            Re: Finding the name of a function while defining it Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-28 01:07 +0000
    Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:45 -0800
      Re: Finding the name of a function while defining it Mitya Sirenef <msirenef@lightbird.net> - 2012-12-27 03:03 -0500
        Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-27 00:26 -0800
          Re: Finding the name of a function while defining it Mitya Sirenef <msirenef@lightbird.net> - 2012-12-27 04:07 -0500
        Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-27 00:26 -0800
      Re: Finding the name of a function while defining it Chris Rebert <clp2@rebertia.com> - 2012-12-27 00:58 -0800
      Re: Finding the name of a function while defining it Tim Roberts <timr@probo.com> - 2012-12-28 21:01 -0800
      Re: Finding the name of a function while defining it Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-12-29 12:18 +0200
  Re: Finding the name of a function while defining it Chris Rebert <clp2@rebertia.com> - 2012-12-27 00:26 -0800
    Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-27 00:42 -0800
    Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-27 00:42 -0800
  Re: Finding the name of a function while defining it Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-27 13:31 +0000
    Re: Finding the name of a function while defining it alex23 <wuwei23@gmail.com> - 2012-12-27 17:25 -0800

csiph-web