Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Finding the name of a function while defining it

Reply
Thread Tools

Finding the name of a function while defining it

 
 
Chris Angelico
Guest
Posts: n/a
 
      12-27-2012
On Thu, Dec 27, 2012 at 6:46 PM, Abhas Bhattacharya
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Abhas Bhattacharya
Guest
Posts: n/a
 
      12-27-2012
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
>
> <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
 
Abhas Bhattacharya
Guest
Posts: n/a
 
      12-27-2012
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
>
> <(E-Mail Removed)> 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?
 
Reply With Quote
 
Mitya Sirenef
Guest
Posts: n/a
 
      12-27-2012
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 <(E-Mail Removed)> 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, http://www.velocityreviews.com/forums/(E-Mail Removed)
>>
>> 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/

 
Reply With Quote
 
Abhas Bhattacharya
Guest
Posts: n/a
 
      12-27-2012
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]()
 
Reply With Quote
 
Abhas Bhattacharya
Guest
Posts: n/a
 
      12-27-2012
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]()
 
Reply With Quote
 
Abhas Bhattacharya
Guest
Posts: n/a
 
      12-27-2012
On Thursday, 27 December 2012 13:56:24 UTC+5:30, Chris Rebert wrote:
> On Dec 25, 2012 6:06 PM, "Abhas Bhattacharya" <(E-Mail Removed)> 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?

>
> Not possible per se without resorting to sys._getframe() or similar hackery.
>
> A simple+elegant way to do this would require PEP 3130 (http://www.python..org/dev/peps/pep-3130/ ) or similar, but that particular proposal got rejected.


Thanks for telling that. I thought that there is a direct way, and now you have confirmed that there isn't. So, as I can see, Mitya's code can be a perfect way-around, although it will require another function.
 
Reply With Quote
 
Abhas Bhattacharya
Guest
Posts: n/a
 
      12-27-2012
On Thursday, 27 December 2012 13:56:24 UTC+5:30, Chris Rebert wrote:
> On Dec 25, 2012 6:06 PM, "Abhas Bhattacharya" <(E-Mail Removed)> 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?

>
> Not possible per se without resorting to sys._getframe() or similar hackery.
>
> A simple+elegant way to do this would require PEP 3130 (http://www.python..org/dev/peps/pep-3130/ ) or similar, but that particular proposal got rejected.


Thanks for telling that. I thought that there is a direct way, and now you have confirmed that there isn't. So, as I can see, Mitya's code can be a perfect way-around, although it will require another function.
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      12-27-2012
On Dec 26, 2012 11:55 PM, "Abhas Bhattacharya" <(E-Mail Removed)>
wrote:
>
> On Thursday, 27 December 2012 10:22:15 UTC+5:30, Tim Roberts wrote:
> > Abhas Bhattacharya <(E-Mail Removed)> wrote:
> >

[Oh god please stop/avoid using Google Groups with its godawful
reply-quoting style that adds excessive blank lines]
> > >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?

<snip>
> 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.

I agree with the general outline of Mitya's suggestion, i.e. refactor the
"print the associated value" step into a separate function, thus obviating
the self-reference issue; it'd be bad to repeat that code in each function
anyway.

Anyhow, here's a simple variation that exploits decorators (because they're
generally awesome & one of my favorite features):

def printing_name_beforehand(func):
def wrapper(*args, **kwargs):
print(the_dict.get(func.__name__))
return func(*args, **kwargs)
return wrapper

Usage:

@printing_name_beforehand
def some_func(...):
# whatever

(Forgive me if there are typos; composing this reply on a tablet is
cumbersome.)

 
Reply With Quote
 
Mitya Sirenef
Guest
Posts: n/a
 
      12-27-2012
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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining a function inside a function. Whats this feature ? How touse inside a class ? Sur Ruby 4 01-08-2008 02:50 PM
Finding defining class in a decorator lcaamano Python 2 05-09-2006 06:20 PM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM
Error while defining char array Boogie El Aceitoso C++ 6 12-03-2003 07:51 PM
Add properties to function while in function with out specifying function ? Richard A. DeVenezia Javascript 4 09-05-2003 01:43 PM



Advertisments