Finding the name of a function while defining it

Chris Angelico rosuav at gmail.com
Thu Dec 27 02:52:45 EST 2012


On Thu, Dec 27, 2012 at 6:46 PM, Abhas Bhattacharya
<abhasbhattacharya2 at 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



More information about the Python-list mailing list