Linked Questions
46 questions linked to/from How to get the caller's method name in the called method?
199 votes
5 answers
159k views
Getting the caller function name inside another function in Python? [duplicate]
If you have 2 functions like: def A def B and A calls B, can you get who is calling B inside B, like: def A () : B () def B () : this.caller.name
0 votes
1 answer
764 views
Python: If-Else Statement That Depends on Calling Function [duplicate]
I've been thinking about this question for a while, and I can't seem to find any related questions to it, probably because I don't know the proper terminology for what I'm looking for. Is there any ...
0 votes
0 answers
118 views
How can I tell what function called my function? [duplicate]
I'm not sure if this is possible, but I'm just wondering if I have code of the form: def f1() return f2() def f2() print(this.calling_function()) What can I put in this.calling_function() to get ...
489 votes
9 answers
309k views
Print current call stack from a method in code
In Python, how can I print the current call stack from within a method (for debugging purposes).
84 votes
6 answers
84k views
Get name and line of calling function in node.js
How can one get the name and line of a function that called the current one? I would like to have a rudimentary debugging function like this (with npmlog defining log.debug): function debug() { var ...
18 votes
8 answers
7k views
Shortcut for if __name__ == '__main__':
Is there a shorter form of this? if __name__ == '__main__': It is pretty tedious to write, and also doesn't look very nice in my opinion :)
12 votes
4 answers
9k views
Python inspect.stack is slow
I was just profiling my Python program to see why it seemed to be rather slow. I discovered that the majority of its running time was spent in the inspect.stack() method (for outputting debug messages ...
10 votes
2 answers
7k views
get a class name of calling method
I know how to get a caller method name (from here: How to get the caller's method name in the called method?) import sys print sys._getframe().f_back.f_code.co_name What I'd like to get is the ...
6 votes
4 answers
7k views
Per-file/module logger in Python
I have some Python code I need to add logging to. I've always preferred nice big C macro looking statements like "DEBUG()", "ERROR()", etc for logging. I feel it makes the code easier to read (no ...
5 votes
4 answers
5k views
Jupyter Notebook: how to find the cell number from which python script function is called?
Suppose I have a Python script called my_script.py with a one function called my_func() inside. I import this script into a Jupyter Notebook so I can run my_func() in the cells. My question is, in ...
5 votes
1 answer
7k views
What exactly is a caller in python?
apologies if this question is too basic/obvious, but I can't find a reasonable answer after searching both here and through the data model docs. My question is simply, what exactly is a caller in ...
2 votes
2 answers
2k views
Can I get a reference to the 'owner' class during the __init__ method of a descriptor?
Is it possible to access the 'owner' class inside a descriptor during the __init__ function of that descriptor, without passing it in manually as in this example? class FooDescriptor(object): def ...
5 votes
2 answers
2k views
How restrict creation of objects of one class to instances of another in Python?
I have encountered a conundrum. I think I should know how to solve this — I am extremely knowledgeable and experienced in Python — but I can't figure it out. It seems like what I am struggling with ...
4 votes
3 answers
3k views
Python: put all function arguments into **kwargs automatically
Description Say I have the following function, which makes a call to another function: def f1(arg1, arg2, arg3): f2(...) The arguments of f1 and f2 are the same, or f2 might look like this: def ...
2 votes
1 answer
2k views
Python create custom magic methods
In python, if I wanted to create an object that did something when passed to a specific function, how could I do that? For example: import logging class TestObject: def __logging__(self): ...