• Python Snacks
  • Posts
  • Functions vs Methods in Python - What's the Difference?

Functions vs Methods in Python - What's the Difference?

See how Python's functions and methods differ with this simple example

Functions and Methods - what is the difference between them?

If you’re on this page, then you’re probably trying to understand the difference between functions and methods.

Good news: you’re in the right spot. In this post, we’ll explore the difference between them with an example using the standard library.

TL;DR, a function is code that performs a specific task that is not defined in a class, whereas a method is a function, but defined in a class and has the ability to modify data within the class.

» Tip: At their core, they work the same way: they contain logic to perform a specific task.

Take the following code, for instance:

Example of functions vs method with code

The key able to really understand the difference between the two, we need to understand the concept of scope. Think of scope as “what part of my code has access to this?”

Understanding Scope

We can think of scope like a library. Libraries are generally accessible to everyone and contains functionality to check out books, use work stations, etc. (think: “scoped to the public; anyone in the world can use the library”)

Let’s say you get into the library and find a table and pull out your laptop. You’re going to need to connect to the WiFi to get some work done.

However, the catch with this is that the WiFi is only for those who are in the library. We can loosely say that the scope of the WiFi is to everyone in the library, but not everyone in the world.

Functions vs Methods: It’s About Scope

Using this example, we can map this to the core differences between functions and methods, appropriately:
- Functions can be thought of the library itself: anyone can access it no matter who you are,
- Methods can be mapped to the WiFi: you need to be in the library first before you can use the WiFi capabilities.

Scope defines where you can access your code.

Differences between methods and functions

Example of methods vs functions using Python

Take a method you may be familiar with, say a list’s .append method:

>>> my_list.append(6) # this won’t work :(

If you try and call this method without defining my_list first, you’ll end up getting an error. This is because instantiating a list is required before calling it:

>>> my_list = []
>>> my_list.append(6) # but this does work!

On the contrary, we can use a Python-built in function without having to create the list first:

>>> sum([1, 2, 3])
6

But, we could create the list first in the above example. However, the sum function does not require the list to be created and can be used independently of a list.

Pro tip: One of the easiest ways to distinguish between a function and a method is with the “dot operator”. For instance, my_list.append(6) has a period between my_list and append, whereas sum does not have a period.

📧 Join the Python Snacks Newsletter! 🐍

Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:

  1. Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.

  2. Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.

  3. Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.

You can unsubscribe at any time.

Interested in starting a newsletter or a blog?

Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.

If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.

This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

Reply

or to participate.