Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it conventional to say that functions are called and methods are invoked?

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?

like image 495
Mahmood Muhammad Nageeb Avatar asked Nov 10 '16 13:11

Mahmood Muhammad Nageeb


People also ask

Are functions and methods the same?

A function is a set of instructions or procedures to perform a specific task, and a method is a set of instructions that are associated with an object.

Why are functions called methods in Java?

Java chose to call them "methods" because they fit the already-existing meaning of that word. Had they called them "functions" they would be introduced confusion because that word already has a different meaning.

What is the difference between methods and functions in Java?

Method and a function are the same, with different terms. A method is a procedure or function in object-oriented programming. A function is a group of reusable code which can be called anywhere in your program. This eliminates the need for writing the same code again and again.

What is the difference between functions and methods in Python?

Functions can be called only by its name, as it is defined independently. But methods can't be called by its name only, we need to invoke the class by a reference of that class in which it is defined, i.e. method is defined within a class and hence they are dependent on that class.


2 Answers

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 :-).

like image 184
Dimitris Fasarakis Hilliard Avatar answered Oct 08 '22 11:10

Dimitris Fasarakis Hilliard


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.

like image 34
elethan Avatar answered Oct 08 '22 11:10

elethan