Why do we not pass anything for self?


#1

Question

Why do we not pass anything for self?

Answer

Python knows that the __init__() method’s first parameter, typically called self, is assigned to the object you create when you initialize it. That way you can use self throughout your class to accomplish things with that particular object!
If you do pass something for self when initializing an object, you’ll get an error about the number of arguments provided and the number of arguments expected. This is because whatever you pass starts by giving value to the second, third, fourth, and so on, parameters.
In our Animal example, we have 4 parameters total: self, name, age, is_hungry. However, if we try to pass it 4 arguments when we call it, we’re actually passing it 5, because it automatically passes self!


FAQ: Learn Python - Introduction to Bitwise Operators - More on __init__() and self