[Tutor] How to have the name of a function inside the code of this function?

Alan Gauld alan.gauld at btinternet.com
Fri Apr 6 19:31:10 CEST 2012


On 06/04/12 09:47, Karim wrote:

> If you have any idea to get the caller name inside the caller.


Its not normally very helpful since in Python the same function can have 
many names:

def F(x):
    return x*x

a = F
b = F
c - lambda y: F(y)

print F(1), a(2), b(3), c(4)

Now, why would knowing whether the caller used F,a or b
to call the same function object help? And what do you
do in the case of c()? Do you return 'c' or 'F'?

Maybe you could use it to tell you the context from
which they were calling? But in that case there are
usually better, more reliable, techniques
  - like examining the stackframe.

HTH,
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list