0

In Python a function takes arguments and optionally returns information.

functionname(param1, param2)
returnvalue = functionname(param1, param2)

A method in Python is called on an object. So, first specify the object, then a dot and then the method you want to execute

objectname.methodname(param, param)

Like Python rfind and many many other methods.

hasattr

hasattr is used like this:

hasattr(my_object, method_to_test_for_existence)

This is clearly a function according to the definition.

Why are some sites calling hasattr a method instead of a function?

  • TestPassport with their first test question. (This one worries me since I'm preparing for the PCAP exam :-) )
  • GeeksForGeeks with the title "Python hasattr() method" (while in their first line they say: 'Python hasattr() function is an inbuilt utility function')
petezurich
  • 9,280
  • 9
  • 43
  • 57
BertC
  • 2,243
  • 26
  • 33
  • 11
    It turns out that people sometimes say method when they mean function. – khelwood Dec 19 '21 at 21:37
  • 8
    A lot of people don't understand the distinction between a function and a method. `hasattr` is a builtin function. – Tom Karzes Dec 19 '21 at 21:37
  • 2
    Relevant: [What's the difference between a method and a function?](https://stackoverflow.com/q/155609/3890632) – khelwood Dec 19 '21 at 21:40
  • 7
    I don't know that first site, but GeeksForGeeks is just horrible in general, don't use it. – Kelly Bundy Dec 19 '21 at 21:42
  • 3
    I second Kelly's comment. *Please* stop using that site immediately. It's not just wrong; it's often comically wrong. The site only exists for SEO to try to get a top spot; it's entirely worthless. – Silvio Mayolo Dec 19 '21 at 21:48
  • 2
    On https://www.testpassport.com/PCAP-31-03-exam.html they have the same question and write *"Explanation: Reference: https://stackoverflow.com/questions/610883/how-to-know-if-an-object-has-an-attribute-in-python". On that page, only one of the low-voted answers calls it a method. And I'm not sure how trustworthy a site is when it cites a stackoverflow question as authoritative source, doesn't strike me as professional :-P – Kelly Bundy Dec 19 '21 at 21:53
  • 7
    "Why are some sites calling hasattr a method instead of a function?" This is really a question for the *authors of those sites*, not for StackOverflow – juanpa.arrivillaga Dec 19 '21 at 21:58
  • 1
    `hasattr()` is a built-in *function* according to its [documentation](https://docs.python.org/3/library/functions.html). – martineau Dec 19 '21 at 22:45
  • Questions about vocabulary are generally off-topic here. Stack Overflow is only for specific problems you actually face in _practicing_ software development (which is to say, problems you face _while writing code_, or whose solutions will help you write better code). See [Questions about terminology related to computer programming](https://meta.stackexchange.com/questions/142723/questions-about-terminology-related-to-computer-programming) on [meta.se]. – Charles Duffy Dec 19 '21 at 22:51

1 Answers1

1

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:

  1. What's the difference between a method and a function? (and my answer to this question)
  2. TutorialsPoint.com: Difference between Method and Function in Python
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • 1
    https://docs.python.org/3/glossary.html#term-method – Kelly Bundy Dec 19 '21 at 23:24
  • 1
    Even the built-in functions list includes a lot of things that, strictly speaking, aren't functions. Many of them are types (`str`, `property`, `map`, `zip`, `int`, etc.); the list would more accurately be called a list of built-in *callables*. (Some of the entries were, historically, functions that became types later.) – chepner Dec 19 '21 at 23:43