Author Options:

What is the purpose of "self" when creating classes in python? Answered

I can't find any good explanations on the internet, so can someone explain why it's needed and where to use it?


The forums are retiring in 2021 and are now closed for new topics and comments.

Best Answer 10 years ago

Hey Evilthingamabober... 

Every method definition in a class starts with a parameter called 'self'. 'self' is just a reference to the current instance, in JavaScript we use 'this'...

When creating an instance of a class that instance contains its own data but not of the methods. When we send a message to an instance and it calls the corresponding method, it does so via an internal reference to the class. It passes a reference to it'self' to the method so that the class code knows which instance to use.

I found this really good explanation of self here