Python's official documentation defines it like this (thank you to @Kelly Bundy here!):
function
A series of statements which returns some value to a caller. It can also be passed zero or more arguments which may be used in the execution of the body. See also parameter, method, and the Function definitions section
method
A function which is defined inside a class body. If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called self
). See function and nested scope.
A square is a rectangle, but not all rectangles are squares. The way I interpret the world, a method is a function, but not all functions are methods. What makes a method unique is that it is a special type of function which also is associated with a class and has access to class member variables.
Your assessment is correct.
The official Python documentation also has hasattr()
in its list of "Built-in Functions" (which are technically actually built-in "callables", since some are callable class objects, not standard functions; thanks @chepner). hasattr()
is only a function, not also a method.
I believe @khelwood is correct:
It turns out that people sometimes say method when they mean function.
People frequently misuses terms, and people are simply erroneously interchanging the terms function and method even though they are subtly different, as a method is a special type of function.
Misusing terms and incorrectly exchanging one for another happens in programming all the time. It seems to me about 90% of programmers misuse, confuse, and inter-change the words "setup" (the noun) and "set up" (the verb). It also happens in plain old English all the time! It seems to me that about 70% of Americans misuse "good" and "well", erroneously interchanging them, as well as "fewer" and "less" ("I have less pancakes than you" is grammatically incorrect--the word fewer should have been used), "there's" vs "there are" (I find myself frequently misusing "there's" in place of "there are"), etc.
Anyway, if you see someone misusing the term "method" when they mean "function", just interpret it correctly in your head and know you are correct :).
Here's an article I found which I think is pretty good. TutorialsPoint isn't always correct, but this article seems to be correct and well-written: TutorialsPoint.com: Difference between Method and Function in Python. (Then again, it's also possible they plagiarized that article from an answer on Stack Overflow, as I've seen them do that before without even giving attribution. Case in point: this TutorialsPoint.com article appears to be directly plagiarized from this Stack Overflow answer. I emailed TutorialsPoint on 23 Oct. 2020, and they replied, "Sure Gabriel, we will look into it.", on 25 Oct. 2020.)
See also:
- What's the difference between a method and a function? (and my answer to this question)
- TutorialsPoint.com: Difference between Method and Function in Python