0

Below is a simple class, which is having one method/function in it

class Test():
    def f(self):
        return "function or method"
 

What should I call f() here - a method or a function?

Braiam
  • 1
  • 11
  • 47
  • 78
Priya Chauhan
  • 445
  • 1
  • 5
  • 21
  • 6
    [You can call it Suzan if it makes you happy](https://www.youtube.com/watch?v=wI7RIRQvEXs). –  Jan 27 '22 at 15:45
  • 1
    https://techvidvan.com/tutorials/python-methods-vs-functions/ – matszwecja Jan 27 '22 at 15:45
  • 3
    method if it has an associated object, function otherwise. But I doubt anyone will be really arguing these semantics with you often. – BTables Jan 27 '22 at 15:46
  • 2
    Functions defined in classes are called [methods](https://en.wikipedia.org/wiki/Method_(computer_programming)). – martineau Jan 27 '22 at 15:48
  • 1
    Does this answer your question? [What's the difference between a method and a function?](https://stackoverflow.com/q/155609/6045800) – Tomerikoo Jan 27 '22 at 15:52

2 Answers2

6

From python official document's glossary:

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.

jupiterbjy
  • 2,882
  • 1
  • 10
  • 28
1

I'd say that is a method, since it is associated with the object of the class that it belongs to. I extracted the information from here.

joangm_
  • 27
  • 1
  • 12