Linked Questions

199 votes
5 answers
159k views

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
Joan Venge's user avatar
  • 334k
0 votes
1 answer
764 views

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 ...
SignalProcessed's user avatar
0 votes
0 answers
118 views

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 ...
cjm2671's user avatar
  • 19.6k
489 votes
9 answers
309k views

In Python, how can I print the current call stack from within a method (for debugging purposes).
oneself's user avatar
  • 40.8k
84 votes
6 answers
84k views

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 ...
Brian M. Hunt's user avatar
18 votes
8 answers
7k views

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 :)
gak's user avatar
  • 33k
12 votes
4 answers
9k views

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 ...
dpitch40's user avatar
  • 2,691
10 votes
2 answers
7k views

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 ...
Philipp Chapkovski's user avatar
6 votes
4 answers
7k views

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 ...
BobIsNotMyName's user avatar
5 votes
4 answers
5k views

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 ...
Jinsong Li's user avatar
  • 7,578
5 votes
1 answer
7k views

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 ...
masiewpao's user avatar
  • 309
2 votes
2 answers
2k views

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 ...
Steven Kampen's user avatar
5 votes
2 answers
2k views

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 ...
Mitchell Model's user avatar
4 votes
3 answers
3k views

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 ...
bagrat's user avatar
  • 7,516
2 votes
1 answer
2k views

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): ...
CaptainDriftwood's user avatar

15 30 50 per page