7

I’m reading Think Python: How to Think Like a Computer Scientist. The author uses “invoke” with methods and “call” with functions.

Is it a convention? And, if so, why is this distinction made? Why are functions said to be called, but methods are said to be invoked?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • Related, possible duplicate (in a C# context): [What's the difference between “call” and “invoke”?](http://stackoverflow.com/q/18505422/464709) – Frédéric Hamidi Nov 10 '16 at 13:34
  • In my experience with Python, I would say it is much more common to say/hear "call" for both methods and functions, and I have rarely heard "invoke" used for either. However, my training in Python is fairly informal, and you might have different conventions in different fields. – elethan Nov 10 '16 at 13:37
  • 2
    I've always differentiated that first order functions are always called. Methods of a class are invoked *on the class* – OneCricketeer Nov 10 '16 at 13:43
  • @cricket_007 interesting. Do recall at all how you picked up that convention? I am wondering if this came from the people/sources you learned from, habits from learning other languages etc.. – elethan Nov 10 '16 at 13:46
  • 1
    @elethan Mostly just my language preference. Not sure if I really picked up anywhere in particular. In functional programming, it's really interchangeable. Say either one, and most people understand, probably. – OneCricketeer Nov 10 '16 at 14:01

2 Answers2

3

Not really, maybe it is easier for new readers to make an explicit distinction in order to understand that their invocation is slightly different. At least that why I suspect the author might have chosen different wording for each.

There doesn't seem to be a convention that dictates this in the Reference Manual for the Python language. What I seem them doing is choosing invoke when the call made to a function is implicit and not explicit.

For example, in the Callables section of the Standard Type Hierarchy you see:

[..] When an instance method object is called, the underlying function (__func__) is called, inserting the class instance (__self__) in front of the argument list. [...]

(Emphasis mine) Explicit call

Further down in Basic Customization and specifically for __new__ you can see:

Called to create a new instance of class cls. __new__() is a static method [...]

(Emphasis mine) Explicit call

While just a couple of sentences later you'll see how invoked is used because __new__ implicitly calls __init__:

If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.

(Emphasis mine) Implicitly called

So no, no convention seems to be used, at least by the creators of the language. Simple is better than complex, I guess :-).

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • 1
    I should add that in Python, every call (except built-in data types, perhaps) includes attribute lookups, so there isn't even a distinction at implementation level. – ivan_pozdeev Nov 10 '16 at 13:49
2

One good source for this would be the Python documentation. A simple text search through the section on Classes reveals the word "call" being used many times in reference to "calling methods", and the word "invoke" being used only once.

In my experience, the same is true: I regularly hear "call" used in reference to methods and functions, while I rarely hear "invoke" for either. However, I assume this is mainly a matter of personal preference and/or context (is the setting informal?, academic?, etc.).

You will also see places in the documentation where the word "invoke" is used in refernce to functions:

void Py_FatalError(const char *message)

Print a fatal error message and kill the process. No cleanup is performed. This function should only be invoked when a condition is detected that would make it dangerous to continue using the Python interpreter; e.g., when the object administration appears to be corrupted. On Unix, the standard C library function abort() is called which will attempt to produce a core file.

And from here:

void Py_DECREF(PyObject *o)

Decrement the reference count for object o. The object must not be NULL; if you aren’t sure that it isn’t NULL, use Py_XDECREF(). If the reference count reaches zero, the object’s type’s deallocation function (which must not be NULL) is invoked.

Although both these references are from the Python C API, so that may be significant.

To summerize:

I think it is safe to use either "invoke" or "call" in the context of functions or methods without sounding either like a noob or a showoff.

Note that I speak only of Python, and what I know from my own experience. I cannot speak to the difference between these terms in other languages.

Community
  • 1
  • 1
elethan
  • 16,408
  • 8
  • 64
  • 87