+ 3
What is the difference between functions and methods in Python?
3 Answers
+ 3
Yep both are the same. Different names though.
+ 2
A function can be called by itself and given arguments to use.
def test(a):
print(a)
test(âaâ)
A method must be called per object. A method can also use properties etc. that are part of the object as well as arguments passed to it. In this sense functions and methods are not quite the same.
class person(name):
self.name = name
def print_details(self, age):
print(self.name, age)
a = person('jim')
b = person('bob')
a.print_name(54)
b.print_name(23)
Both functions and methods can use globals.
A method could use a function, but a function may not use a method (as is). Method has to be called from the object. A method can also be called from inside the object by using self (self.method_name(a, b, c))
In summary - they are both functions, however their usage can be quite different.
0
Methods are members of classes/objects. Basically the same thing though.