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.


Greg Kitchin
Greg Kitchin
31,138 Points

Difference between functions and methods in Python

I've just started the new Python course, and a little confused over how a function and method are different. The way they're described, they sound similar, but am I right in saying a method would be specific to an object, while a function is more generic in relation to the overall program? I'm guessing they're created in a different manner as well, but the way they're described is throwing me a little.

I've done a bit of Java, so that's where I'm coming from, but thought I better ask this.

1 Answer

Ken Alger
Ken Alger
Treehouse Teacher


I think you are understanding things well, but let me try.

A function is a piece of code that is called by name. It can be passed data to operate on (i.e., the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed.

def sum(num1, num2):
    return num1 + num2

A method is a piece of code that is called by name that is associated with an object. In most respects it is identical to a function except for two key differences.

  1. It is implicitly passed for the object for which it was called.
  2. It is able to operate on data that is contained within the class (remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data).
class Dog:
    def my_method(self):
        print "I am a Dog"

dog = Dog()
dog.my_method()  # Prints "I am a Dog"

(this is a simplified explanation, ignoring issues of scope etc.)

Post back if you still have questions.

Happy coding,