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


Groups > comp.lang.python > #35505 > unrolled thread

Finding the name of a function while defining it

Started byAbhas Bhattacharya <abhasbhattacharya2@gmail.com>
First post2012-12-25 18:00 -0800
Last post2012-12-27 17:25 -0800
Articles 20 on this page of 32 — 10 participants

Back to article view | Back to comp.lang.python


Contents

  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

Page 1 of 2  [1] 2  Next page →


#35505 — Finding the name of a function while defining it

FromAbhas Bhattacharya <abhasbhattacharya2@gmail.com>
Date2012-12-25 18:00 -0800
SubjectFinding the name of a function while defining it
Message-ID<c9548d77-ccc3-4b47-b84b-9a9f0c2852ce@googlegroups.com>
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?

[toc] | [next] | [standalone]


#35507

FromRoy Smith <roy@panix.com>
Date2012-12-25 22:11 -0500
Message-ID<roy-F99DCA.22112825122012@news.panix.com>
In reply to#35505
In article <c9548d77-ccc3-4b47-b84b-9a9f0c2852ce@googlegroups.com>,
 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?

Do you need it at compile-time, or is it good enough to have the name a 
run-time?  Assuming the latter, then I'm thinking the traceback module 
is your friend.  Call traceback.extract_stack() and pull off the last 
frame in the stack.  The function name will be in there.

There may be a cleaner way, but that's what I've done in the past.

I've only ever wanted the name.  If you need the actual function object, 
I suppose you might eval() the name, or something like that.

[toc] | [prev] | [next] | [standalone]


#35587

FromAbhas Bhattacharya <abhasbhattacharya2@gmail.com>
Date2012-12-26 23:26 -0800
Message-ID<9ca1c0f1-fd79-4525-a571-64f1efb2eefa@googlegroups.com>
In reply to#35507
On Wednesday, 26 December 2012 08:41:28 UTC+5:30, Roy Smith  wrote:
> In article <c9548d77-ccc3-4b47-b84b-9a9f0c2852ce@googlegroups.com>,
> 
>  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?
> 
> 
> 
> Do you need it at compile-time, or is it good enough to have the name a 
> 
> run-time?  Assuming the latter, then I'm thinking the traceback module 
> 
> is your friend.  Call traceback.extract_stack() and pull off the last 
> 
> frame in the stack.  The function name will be in there.
> 
> 
> 
> There may be a cleaner way, but that's what I've done in the past.
> 
> 
> 
> I've only ever wanted the name.  If you need the actual function object, 
> 
> I suppose you might eval() the name, or something like that.

I need it compile-time.
During run-time, I can always use: function_name.__name__ (although that's kind of lame because it returns "function_name"). But if the function itself contains print(__name__) and I call the function, it returns __main__ (yes, __main__ itself, not the string "__main__") (which is the calling function).

[toc] | [prev] | [next] | [standalone]


#35591

FromChris Angelico <rosuav@gmail.com>
Date2012-12-27 18:48 +1100
Message-ID<mailman.1329.1356594507.29569.python-list@python.org>
In reply to#35587
On Thu, Dec 27, 2012 at 6:26 PM, Abhas Bhattacharya
<abhasbhattacharya2@gmail.com> wrote:
> During run-time, I can always use: function_name.__name__ (although that's kind of lame because it returns "function_name"). But if the function itself contains print(__name__) and I call the function, it returns __main__ (yes, __main__ itself, not the string "__main__") (which is the calling function).

That's because __name__ looks for that attribute on the module (aka
"global variable"), not the function. When you run your Python script
as an application, the module is called __main__.

ChrisA

[toc] | [prev] | [next] | [standalone]


#35593

FromAbhas Bhattacharya <abhasbhattacharya2@gmail.com>
Date2012-12-26 23:52 -0800
Message-ID<94373e57-3564-4991-8462-25d4ed74dcf5@googlegroups.com>
In reply to#35591
On Thursday, 27 December 2012 13:18:19 UTC+5:30, Chris Angelico  wrote:
> On Thu, Dec 27, 2012 at 6:26 PM, Abhas Bhattacharya
> 
> <abhasbhattacharya2@gmail.com> wrote:
> 
> > During run-time, I can always use: function_name.__name__ (although that's kind of lame because it returns "function_name"). But if the function itself contains print(__name__) and I call the function, it returns __main__ (yes, __main__ itself, not the string "__main__") (which is the calling function).
> 
> 
> 
> That's because __name__ looks for that attribute on the module (aka
> 
> "global variable"), not the function. When you run your Python script
> 
> as an application, the module is called __main__.
> 
> 
> 
> ChrisA

Ok, that sheds some light on why it acts like that, but how can i use it the way I want (which i already told)?

[toc] | [prev] | [next] | [standalone]


#35594

FromAbhas Bhattacharya <abhasbhattacharya2@gmail.com>
Date2012-12-26 23:52 -0800
Message-ID<mailman.1330.1356594753.29569.python-list@python.org>
In reply to#35591
On Thursday, 27 December 2012 13:18:19 UTC+5:30, Chris Angelico  wrote:
> On Thu, Dec 27, 2012 at 6:26 PM, Abhas Bhattacharya
> 
> <abhasbhattacharya2@gmail.com> wrote:
> 
> > During run-time, I can always use: function_name.__name__ (although that's kind of lame because it returns "function_name"). But if the function itself contains print(__name__) and I call the function, it returns __main__ (yes, __main__ itself, not the string "__main__") (which is the calling function).
> 
> 
> 
> That's because __name__ looks for that attribute on the module (aka
> 
> "global variable"), not the function. When you run your Python script
> 
> as an application, the module is called __main__.
> 
> 
> 
> ChrisA

Ok, that sheds some light on why it acts like that, but how can i use it the way I want (which i already told)?

[toc] | [prev] | [next] | [standalone]


#35615

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-12-27 12:59 +0000
Message-ID<50dc461d$0$29967$c3e8da3$5496439d@news.astraweb.com>
In reply to#35507
On Tue, 25 Dec 2012 22:11:28 -0500, Roy Smith wrote:

> I've only ever wanted the name.  If you need the actual function object,
> I suppose you might eval() the name, or something like that.

Oh look, I found a peanut! Let me get a 50lb sledgehammer to crack it 
open!

*wink*

Please do not use eval to retrieve the function object. It's slow, 
overkill, a bad habit to get into, and a security risk if the name you 
are eval'ing comes from an untrusted source.

Instead, look the name up in globals() or locals():


py> def spam():
...     return "NOBODY expects the Spanish Inquisition!"
...
py> name = "spam"
py> func = globals()[name]
py> func()
'NOBODY expects the Spanish Inquisition!'



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#35573

FromTim Roberts <timr@probo.com>
Date2012-12-26 20:52 -0800
Message-ID<0kknd8tbg7knqa1ng6igbj8u82mqb720oi@4ax.com>
In reply to#35505
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.

[toc] | [prev] | [next] | [standalone]


#35579

FromChris Angelico <rosuav@gmail.com>
Date2012-12-27 16:44 +1100
Message-ID<mailman.1323.1356587079.29569.python-list@python.org>
In reply to#35573
On Thu, Dec 27, 2012 at 3:52 PM, Tim Roberts <timr@probo.com> wrote:
> 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?

Presumably 'one'.

> Even more obscure:
>
> two = lamba : "one"
> one = two
>
> Which one of these is the "name" of the function?

I would say '<lambda>'. Whatever method is used to get the function's
name, I would expect it to match the __name__ attribute of the
function (which is a peer to __code__, but I don't think the
function's code *is* the function).

ChrisA

[toc] | [prev] | [next] | [standalone]


#35589

FromAbhas Bhattacharya <abhasbhattacharya2@gmail.com>
Date2012-12-26 23:46 -0800
Message-ID<442ad592-1435-4be2-820d-970f7c8b86d0@googlegroups.com>
In reply to#35579
On Thursday, 27 December 2012 11:14:36 UTC+5:30, Chris Angelico  wrote:
> On Thu, Dec 27, 2012 at 3:52 PM, Tim Roberts <timr@probo.com> wrote:
> 
> > 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?
> 
> 
> 
> Presumably 'one'.
> 
> 
> 
> > Even more obscure:
> 
> >
> 
> > two = lamba : "one"
> 
> > one = two
> 
> >
> 
> > Which one of these is the "name" of the function?
> 
> 
> 
> I would say '<lambda>'. Whatever method is used to get the function's
> 
> name, I would expect it to match the __name__ attribute of the
> 
> function (which is a peer to __code__, but I don't think the
> 
> function's code *is* the function).
> 
> 
> 
> ChrisA

If i call one() and two() respectively, i would like to see "one" and "two".

[toc] | [prev] | [next] | [standalone]


#35595

FromChris Angelico <rosuav@gmail.com>
Date2012-12-27 18:52 +1100
Message-ID<mailman.1331.1356594775.29569.python-list@python.org>
In reply to#35589
On Thu, Dec 27, 2012 at 6:46 PM, Abhas Bhattacharya
<abhasbhattacharya2@gmail.com> wrote:
> [ a whole lot of double-spaced quoted text - please trim it ]
> If i call one() and two() respectively, i would like to see "one" and "two".

That completely goes against your idea of knowing at compile-time,
because the name "two" isn't anywhere around at that time.

There's no way to know what name was used to look something up. It
might not even have a name - the called function could well have been
returned from another function:

# foo.py
def indirection():
  return lambda: print

# bar.py
import foo
foo.indirection()()("Hello, world!")

What are the names of all the functions called here?

ChrisA

[toc] | [prev] | [next] | [standalone]


#35596

FromAbhas Bhattacharya <abhasbhattacharya2@gmail.com>
Date2012-12-26 23:55 -0800
Message-ID<6a24450c-2ec4-4cbc-a2c1-299230b3e04c@googlegroups.com>
In reply to#35595
On Thursday, 27 December 2012 13:22:45 UTC+5:30, Chris Angelico  wrote:
> On Thu, Dec 27, 2012 at 6:46 PM, Abhas Bhattacharya
> 
> <abhasbhattacharya2@gmail.com> wrote:
> 
> > [ a whole lot of double-spaced quoted text - please trim it ]
> 
> > If i call one() and two() respectively, i would like to see "one" and "two".
> 
> 
> 
> That completely goes against your idea of knowing at compile-time,
> 
> because the name "two" isn't anywhere around at that time.
> 
> 
> 
> There's no way to know what name was used to look something up. It
> 
> might not even have a name - the called function could well have been
> 
> returned from another function:
> 
> 
> 
> # foo.py
> 
> def indirection():
> 
>   return lambda: print
> 
> 
> 
> # bar.py
> 
> import foo
> 
> foo.indirection()()("Hello, world!")
> 
> 
> 
> What are the names of all the functions called here?
> 
> 
> 
> ChrisA

Yes, I get it that it may not be possible in complex cases (mostly using lambda functions). But in the simple case I mentioned, is it possible?

[toc] | [prev] | [next] | [standalone]


#35597

FromAbhas Bhattacharya <abhasbhattacharya2@gmail.com>
Date2012-12-26 23:55 -0800
Message-ID<mailman.1332.1356594951.29569.python-list@python.org>
In reply to#35595
On Thursday, 27 December 2012 13:22:45 UTC+5:30, Chris Angelico  wrote:
> On Thu, Dec 27, 2012 at 6:46 PM, Abhas Bhattacharya
> 
> <abhasbhattacharya2@gmail.com> wrote:
> 
> > [ a whole lot of double-spaced quoted text - please trim it ]
> 
> > If i call one() and two() respectively, i would like to see "one" and "two".
> 
> 
> 
> That completely goes against your idea of knowing at compile-time,
> 
> because the name "two" isn't anywhere around at that time.
> 
> 
> 
> There's no way to know what name was used to look something up. It
> 
> might not even have a name - the called function could well have been
> 
> returned from another function:
> 
> 
> 
> # foo.py
> 
> def indirection():
> 
>   return lambda: print
> 
> 
> 
> # bar.py
> 
> import foo
> 
> foo.indirection()()("Hello, world!")
> 
> 
> 
> What are the names of all the functions called here?
> 
> 
> 
> ChrisA

Yes, I get it that it may not be possible in complex cases (mostly using lambda functions). But in the simple case I mentioned, is it possible?

[toc] | [prev] | [next] | [standalone]


#35590

FromAbhas Bhattacharya <abhasbhattacharya2@gmail.com>
Date2012-12-26 23:46 -0800
Message-ID<mailman.1328.1356594400.29569.python-list@python.org>
In reply to#35579
On Thursday, 27 December 2012 11:14:36 UTC+5:30, Chris Angelico  wrote:
> On Thu, Dec 27, 2012 at 3:52 PM, Tim Roberts <timr@probo.com> wrote:
> 
> > 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?
> 
> 
> 
> Presumably 'one'.
> 
> 
> 
> > Even more obscure:
> 
> >
> 
> > two = lamba : "one"
> 
> > one = two
> 
> >
> 
> > Which one of these is the "name" of the function?
> 
> 
> 
> I would say '<lambda>'. Whatever method is used to get the function's
> 
> name, I would expect it to match the __name__ attribute of the
> 
> function (which is a peer to __code__, but I don't think the
> 
> function's code *is* the function).
> 
> 
> 
> ChrisA

If i call one() and two() respectively, i would like to see "one" and "two".

[toc] | [prev] | [next] | [standalone]


#35609

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-12-27 10:58 +0000
Message-ID<50dc29e9$0$29967$c3e8da3$5496439d@news.astraweb.com>
In reply to#35590
On Wed, 26 Dec 2012 23:46:31 -0800, Abhas Bhattacharya wrote:

>> > two = lamba : "one"
>> > one = two
>> 
>> > Which one of these is the "name" of the function?
[...]
> If i call one() and two() respectively, i would like to see "one" and
> "two".

I'm afraid you're going to be disappointed. There is no possible way for 
one() and two() as shown above to report different names, because they 
are the same function object.

py> two = lambda : "one"
py> one = two
py> one is two
True
py> one, two
(<function <lambda> at 0xb7abd92c>, <function <lambda> at 0xb7abd92c>)



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#35621

FromTim Chase <python.list@tim.thechases.com>
Date2012-12-27 07:32 -0600
Message-ID<mailman.1345.1356615069.29569.python-list@python.org>
In reply to#35609
On 12/27/12 04:58, Steven D'Aprano wrote:
> On Wed, 26 Dec 2012 23:46:31 -0800, Abhas Bhattacharya wrote:
> 
>>>> two = lamba : "one"
>>>> one = two
>>>
>>>> Which one of these is the "name" of the function?
> [...]
>> If i call one() and two() respectively, i would like to see "one" and
>> "two".
> 
> I'm afraid you're going to be disappointed. There is no possible way for 
> one() and two() as shown above to report different names, because they 
> are the same function object.
> 
> py> two = lambda : "one"
> py> one = two
> py> one is two
> True
> py> one, two
> (<function <lambda> at 0xb7abd92c>, <function <lambda> at 0xb7abd92c>)

And for similar fun:

  def call(fn, *args, **kwargs):
    return fn(*args, **kwargs)

  two = lambda : "one"
  one = two
  print(call(two))
  print(call(one))

Depending on where in the code you are, the same function object
also has a local name of "fn".  It's madness until you understand
it, and then it's beauty :)

-tkc

[toc] | [prev] | [next] | [standalone]


#35622

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-12-27 13:34 +0000
Message-ID<50dc4e4e$0$29967$c3e8da3$5496439d@news.astraweb.com>
In reply to#35621
On Thu, 27 Dec 2012 07:32:16 -0600, Tim Chase wrote:

> Depending on where in the code you are, the same function object also
> has a local name of "fn".  It's madness until you understand it, and
> then it's beauty :)

"This is madness!"

"No, this is PYTHON!!!"



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#35624

FromRoy Smith <roy@panix.com>
Date2012-12-27 10:09 -0500
Message-ID<roy-1A707B.10090127122012@news.panix.com>
In reply to#35609
In article <50dc29e9$0$29967$c3e8da3$5496439d@news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:

> On Wed, 26 Dec 2012 23:46:31 -0800, Abhas Bhattacharya wrote:
> 
> >> > two = lamba : "one"
> >> > one = two
> >> 
> >> > Which one of these is the "name" of the function?
> [...]
> > If i call one() and two() respectively, i would like to see "one" and
> > "two".
> 
> I'm afraid you're going to be disappointed. There is no possible way for 
> one() and two() as shown above to report different names, because they 
> are the same function object.

Well, there is the (yes, I know it's absurd) sledgehammer-and-peanut way 
of getting a stack trace, finding the frame that called your function, 
and parsing the text of that line.

Never tell a hacker, "no possible way" :-)

[toc] | [prev] | [next] | [standalone]


#35665

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-12-28 01:07 +0000
Message-ID<50dcf0dd$0$29967$c3e8da3$5496439d@news.astraweb.com>
In reply to#35624
On Thu, 27 Dec 2012 10:09:01 -0500, Roy Smith wrote:

> In article <50dc29e9$0$29967$c3e8da3$5496439d@news.astraweb.com>,
>  Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
> 
>> On Wed, 26 Dec 2012 23:46:31 -0800, Abhas Bhattacharya wrote:
>> 
>> >> > two = lamba : "one"
>> >> > one = two
>> >> 
>> >> > Which one of these is the "name" of the function?
>> [...]
>> > If i call one() and two() respectively, i would like to see "one" and
>> > "two".
>> 
>> I'm afraid you're going to be disappointed. There is no possible way
>> for one() and two() as shown above to report different names, because
>> they are the same function object.
> 
> Well, there is the (yes, I know it's absurd) sledgehammer-and-peanut way
> of getting a stack trace, finding the frame that called your function,
> and parsing the text of that line.
> 
> Never tell a hacker, "no possible way" :-)


1) That is not supported by Python the language, only by certain Python 
implementations. You might be running IronPython with frame support 
turned off, or some other implementation with no frames at all.


2) Even if you have frames, you might not have access to the source code. 
The code may be running from the (CPython) interactive interpreter, or 
from a .pyc or .pyo file.


3) Even if you have the text of the source code, it might be ambiguous 
(e.g. "f(); g(); one(); two()") and you cannot tell which is "the 
current" function call.


4) Or the code may be obfuscated and too hard for you to parse:

eval("eval(''.join('ronnie'[1::2])+chr(40)+chr(41))")

(I expect that you, a human intelligence, can decipher the above, but can 
your parser?)


5) Which also rules out decompiling the byte code and parsing the pseudo-
assembly.

No matter how clever your parser, a sufficiently obfuscated algorithm 
will defeat it.


When I say something is impossible, I mean it is not a promised by the 
language. Naturally there may be ways of accomplishing a task that the 
language does not promise to allow which "only sometimes" works. At 
worst, you can always guess an answer, and hope that you're right!

def get_current_function_name():
    # Only sometimes right.
    return "one" if random.random() < 0.5 else "two"


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#35588

FromAbhas Bhattacharya <abhasbhattacharya2@gmail.com>
Date2012-12-26 23:45 -0800
Message-ID<c9189a65-74ee-4ea7-a4b6-33ece392ea8f@googlegroups.com>
In reply to#35573
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.

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web