Can it be argued that functions are called and methods are used?

Im reading Think Python: how to think like a computer scientist. The author uses โ€œcallโ€ with methods and โ€œcallโ€ with functions.

Is this a convention? And if so, why is this distinction made? Why are functions called called , but methods are called called ?

+6
source share
2 answers

Not really, maybe itโ€™s easier for new readers to make a clear distinction to understand that their appeal is slightly different. At least why I suspect that the author could choose different formulations for each of them.

There does not seem to be a convention that dictates this in the Python reference manual. It seems that they do this by calling a call when a call made to a function is implicit and not explicit.

For example, in the Callables section of the standard type hierarchy, you see:

[..] When the instance method object is called , the main function ( __func__ ) is __func__ , inserting the class instance ( __self__ ) before the argument list. [...]

(Emphasis mine) explicit call

Further in the Basic setup and, in particular, for __new__ you can see:

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

(Emphasis mine) explicit call

While only a few sentences later you will see how invoked is used, because __new__ implicitly calls __init__ :

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

(Emphasis mine) Implicitly called

So no, not a single agreement is used, at least by the creators of the language. Simple is better than complex, I think :-).

+2
source

One good source for this would be the Python documentation . A simple text search in the "Classes" section shows that the word "call" is used many times in relation to "calling methods", and the word "call" is used only once.

In my experience, the same is true: I regularly hear the โ€œcallโ€ used with respect to methods and functions, while I rarely hear the โ€œinvokeโ€ for both. Nevertheless, I believe that this is mainly a matter of personal preference and / or context (is this an informal setting ?, Academic? Etc.).

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

void Py_FatalError (const char * message)

Print a fatal error message and kill the process. No cleaning is performed. This function should only be called when a condition is found that makes it dangerous to continue to use the Python interpreter; for example, when the administration of an object seems to be corrupted. On Unix standard C, the abort () function of the library is called, which will try to create the main file.

And from here :

void Py_DECREF (PyObject * o)

Decrease the reference count for the o object. The object must not be NULL; if you are not sure if it is not NULL, use Py_XDECREF (). If the reference count reaches zero, objects of type deallocation (which should not be NULL) are called .

Although both of these links are from the Python C API, this can be significant.

For summerize:

I think it's safe to use either "invoke" or "call" in the context of functions or methods without sounding either as noob or showoff.

Please note that I am only talking about Python and what I know from my own experience. I can not talk about the difference between these terms in other languages.

+1
source

Source: https://habr.com/ru/post/1012221/


All Articles