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 Object-Oriented Python (retired) Inheritance DRY

Pulkit Kumar
Pulkit Kumar
2,052 Points

just a very basic question why do we always have to pass self in the methods that we define?

why do we always have to pass self in the methods that we define?

2 Answers

Josip Dorvak
Josip Dorvak
18,126 Points

I would take a look at this page: (http://stackoverflow.com/questions/2709821/python-self-explained)

It explains alot on why self is needed

However a basic runthrough is that if you have a function:

def function(self, arg1):
   ....

from a class called ClassA and an object of ClassA called ObjA,

when you call ObjA.function(2), what really happens behing the scenes is this

ClassA.function(ObjA, 2)

the function needs to have a reference to the object that is calling it

Tobias Edwards
Tobias Edwards
14,458 Points

I try and think of it to show that these methods are linked to our class. I recommend doing some independent research on self, as I have done (it took me 20 mins to get the hang of it).