Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
Jonathan Fernandes
Courses Plus Student 22,783 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):

def some_function():

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.