0

I can't seem to understand the difference between methods and functions.

From my knowledge I am aware that methods are functions that are unique to the classes they are implemented in, but for functions can I say that they can be used in general and are not restricted to a certain class. Also, is the indentation of functions vs methods another essential difference? As methods are implemented within classes and functions are outside with the least indentation.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Does this answer your question? [Difference between methods and functions, in Python compared to C++](https://stackoverflow.com/questions/20981789/difference-between-methods-and-functions-in-python-compared-to-c) – mkrieger1 Feb 15 '23 at 22:17

2 Answers2

2

A function is virtually the same as a method only that the latter is bound to a class. In Python, in most cases, the same way you define a function is the same way you define a method. However, to refer to the class it is in, you will at times see the 'self' parameter added to the method as in: def function_name(self):. The indentation works similarly in both cases.

I think the reason why you tend to think that the indentation of a method is deeper than that of a function is because by the time you're writing your method, you are already indented inside the class.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • 1
    Really sorry if I am writing a bunch of nonsense but python is still new to me and I'm trying to learn. But from what I can infer is that methods are bounded to the class and functions are independent from any class? Also the way that we utilise functions and methods are different too? – randomvlad Feb 15 '23 at 03:55
0

Python, in particular, across most programming languages, really does not differentiate much between functions and methods.

What you declare is true: "methods are functions that are unique to the classes they are implemented in" - And that makes for the indentation difference: since methods are inside a class, they are indented inside the class statement block.

When you create a simple class, and retrieve a method from it directly, it is actually a function (in python 3 - it worked differently in old Python 2.x):

from types import FunctionType

class A:
    def b(self): 
        pass

print(A.b, type(A.b) is FunctionType) 

Prints:

<function A.b at 0x...> True

So, in Python, a "method" is really and literally a function! It will work differently when retrieved from an instance of the class it is declared on.

[optional complicated paragraph] Then, Python wraps it with what can be described as "a lazy object callable that will insert the instance it is bound to as first argument to the underlying function when called." Once you can make sense of this phrase, you can check "seasoned Pythonista" in your CV.

What it means is simply that "self" will appear "magically" when calling the method, and moreover, one can go back to the original function by looking at the method's .__func__ attribute.

So, given the same class A above:

In [52]: print(A().b, type(A().b), type(A().b) is FunctionType)
<bound method A.b of <__main__.A object at 0x...>> <class 'method'> False

In [53]: print(A().b.__func__, type(A().b.__func__), type(A().b.__func__) is FunctionType)
<function A.b at 0x...> <class 'function'> True

Note that in these examples I instantiate a new object of the class A at each time, by writing A(), while in the first example, I retrieve b directly from the class, without parenthesis: A.b

As I stated in the beggining of the text: be aware that the fact a plain function can be used as a method is particular to Python, and both kinds of objects will likely have some differences in other programming languages. The idea however, will remain the same: a method will always "know" about the instance from where it was retrieved.

jsbueno
  • 99,910
  • 10
  • 151
  • 209