5

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 python 3? Is there a strict definition?

I know for example a function that calls another function is known as a caller. So:

def f1():
    pass
def f2():
    f1()

f2 is the caller of f1. But what about assignment statements?

x = f2()

Is x a caller of f2? Is it also a caller of f1? It's obviously been stated that the return statement at the end of a function definition returns a value to the caller, so I would assume in this case x is both a caller of f2 and f1, however I just want to check that there is no deeper/technical meaning to what a caller is?

I found this question I don't understand "return" in Python and what is a caller?, but I couldn't get much understanding.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
masiewpao
  • 309
  • 3
  • 12
  • 3
    I would say `f1` calls `f2` and `x` is the result of `f2()` - I would not call anyone "caller" - what does this matter for? – Patrick Artner Nov 26 '18 at 17:16
  • Hello, I am not sure I understand why f1 calls f2? https://stackoverflow.com/questions/2654113/how-to-get-the-callers-method-name-in-the-called-method This question seemed to imply that it should be the other way around, but I'm not too sure. There's no practical reason, but I keep seeing caller being used but I realised I don't actually know exactly what it means! – masiewpao Nov 26 '18 at 17:19
  • 1
    ups ... typo --- f2 calls f1 , sorry for the confusion- I somewhen got a degree in CS and I do not think I ever adressed functions as caller or callee :) – Patrick Artner Nov 26 '18 at 17:25
  • AFAIK *"caller"* just refers to where the call occurred, not necessarily something in particular. Although I still don't understand what you're actually asking – Nick is tired Nov 26 '18 at 17:25
  • @PatrickArtner ah ok, thank you! – masiewpao Nov 26 '18 at 17:26
  • @NickA I was afraid the question would be too vague. I am basically wondering if there is a technical meaning of 'caller' in the same way there is a strict technical meaning of 'iterator' in python. I am not sure if it's an inappropriate question since there is no practical application to this (that I can think of), I just can't find a resource that describes whether or not a 'caller' is colloquial or a technical definition. I hope that helps. – masiewpao Nov 26 '18 at 17:28
  • 1
    @masiewpao That's fair. Personally I have never used the term or heard it used (although I have heard *"calling function"* and similar) – Nick is tired Nov 26 '18 at 17:30
  • 1
    I would say that `f2` is a caller, but note that not every function call *has* a caller in that case; there's no requirement that every function call occurs inside another function. – chepner Nov 26 '18 at 17:34

1 Answers1

4

The caller of a function is the unit of program code in which the call to the function occurred. This can be another function, a method (a specific type of function) or, in Python, the code in the "top level" of a python source code file -- generally referred to as a script.

Larry Lustig
  • 49,320
  • 14
  • 110
  • 160