Python Methods vs Functions — What's the Difference_
Python Methods vs Functions — What's the Difference_
function(object)
object.method()
https://www.codingem.com/python-what-is-the-difference-between-a-function-and-a-method/ 1/4
16/2/25, 1:02 p.m. Python Methods vs Functions — What's the Difference?
Let’s take a look at a table that summarizes the main differences between methods and
functions in Python. Notice that most of these differences apply to other programming languages
too.
Methods Functions
https://www.codingem.com/python-what-is-the-difference-between-a-function-and-a-method/ 2/4
16/2/25, 1:02 p.m. Python Methods vs Functions — What's the Difference?
Methods Functions
A method operates on the data of the object it A function operates on the data
belongs to. you give it as an argument.
A method always requires ‘self‘ as the first A function doesn’t take ‘self‘ as
argument. an argument.
For example:
print("Test")
To demonstrate methods next, let’s continue with the string data type.
The string type str has a ton of built-in methods. One of which is the upper() method that
converts the string object into uppercase.
string = "Test"
upper_string = string.upper()
class Weight():
weight = 100
# Defining a method
def to_pounds(self):
return 2.205 * self.weight
# Defining a function
def to_pounds(kilos):
return 2.205 * kilos
# Calling a method on an object.
w = Weight()
pounds = w.to_pounds()
# Calling a function on an object
kilos = 100
pounds = to_pounds(kilos)
Conclusion
Today you learned what is the difference between a function and a method in Python.
Happy coding!
https://www.codingem.com/python-what-is-the-difference-between-a-function-and-a-method/ 4/4