Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics Types and Branching String Methods

Is len() a method or a function?

I'm running through the course again, and the terminology is confusing me once again.

1 Answer

Jonathan Fernandes
PLUS
Jonathan Fernandes
Courses Plus Student 22,784 Points

So the wording is tricky so I will try my best to explain. Let me know if you want me to clarify further.

The short answer: it is both. Len() is a method, and therefore also a function.

Generally speaking, a method is a function that is usually associated with a class or object. So it would be true to say that all methods are functions but not all functions are methods, since you can create a function outside of a class or object that will not be associated with either.

In this particular case, the len() method is actually associated with Python itself. Python comes out of the box with some ready to go methods to help developers, such as str(), int(), etc. This makes it a Python method.

Here is a simple example of two functions, one associated with a class and another that is not:

class SomeClass:

    def some_function(self):
         pass

def some_function():
    pass

The first belongs to the SomeClass class so it is a method of that class. The second belongs to nothing so it is just a function.

Hope that helps!!!

This helps a lot, thanks! The words are used interchangeably at times and that causes me a lot of confusion.