0% found this document useful (0 votes)
4 views

Python Methods vs Functions — What's the Difference_

The document explains the key differences between functions and methods in Python, highlighting that functions are defined outside of classes while methods are defined within classes and associated with objects. It provides examples of calling functions and methods, such as the print() function and the upper() method of a string. The conclusion reiterates that functions are independent blocks of code, whereas methods operate on the data of their associated objects.

Uploaded by

dardanus77
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Methods vs Functions — What's the Difference_

The document explains the key differences between functions and methods in Python, highlighting that functions are defined outside of classes while methods are defined within classes and associated with objects. It provides examples of calling functions and methods, such as the print() function and the upper() method of a string. The conclusion reiterates that functions are independent blocks of code, whereas methods operate on the data of their associated objects.

Uploaded by

dardanus77
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

16/2/25, 1:02 p.m. Python Methods vs Functions — What's the Difference?

Python Methods vs Functions — What’s


the Difference?
By Artturi Jalli

The key difference between a function and a method in Python is:

1. A function is implemented outside of a class. It does not belong to an object.


2. A method is implemented inside of a class. It belongs to an object.

Calling a function on an object looks like this:

function(object)

And calling a method of an object looks like this:

object.method()

Here is a simple illustration of this key difference:

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.

Functions vs Methods in Python—A


Comparison Table
Here is a comprehensive comparison table that compares the key differences between methods
and functions in Python.

Methods Functions

A method lives in a class. A function lives outside classes.

A function is not associated with


A method is associated with a class object.
any objects.

You can only call a method on an object.


You can call a function by only
Calling a method by its name only is not
using its name.
possible.

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 is dependent on the object it belongs A function is an independent


to. block of code in the program.

A method always requires ‘self‘ as the first A function doesn’t take ‘self‘ as
argument. an argument.

Function vs Method Example in Python


Probably the most famous function in Python is the print() function. You can call it on any
object to output the object as text in the console.

To print a string, for example, call print() function on a string.

For example:

print("Test")

This is a great example of calling a function on an object in Python.

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.

To convert a string into uppercase, call the upper() method of a string.

string = "Test"
upper_string = string.upper()

This is a great example of calling a method on an object in Python.

Methods vs Functions: A Code Example


Here is a simple code example of a class with a method. Outside the class, there is a function
with the same name.
https://www.codingem.com/python-what-is-the-difference-between-a-function-and-a-method/ 3/4
16/2/25, 1:02 p.m. Python Methods vs Functions — What's the Difference?

Please read the code comments to understand what’s going on.

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.

A function does not belong to a class. It is implemented outside of a class.


A method belongs to a class and it can only be called on objects of that class. A
method is implemented inside of a class.

An example of a function in Python is the print() function.

An example of a commonly used method in Python is the upper() method of a string.

Thanks for reading. I hope you find it useful.

Happy coding!

https://www.codingem.com/python-what-is-the-difference-between-a-function-and-a-method/ 4/4

You might also like