+ 3
What is the difference between function and method in python?
3 Answers
+ 2
A function is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed.
A method is a piece of code that is called by a name that is associated with an object. In most respects it is identical to a function except for two key differences:
A method is implicitly passed the object on which it was called.A method is able to operate on data that is contained within the class (remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data).
+ 4
a function define a name that defines some or 1 line of codes
a method is like a function but it is related to a class
+ 1
for example:
1) METHOD: append is the method in lists class we can call it with dot operator
variable.append(value)
2) FUNCTION: On the other hand len is the function from lists class we pass parameters directly to it
len(variable)