It took me a while to understand this concept. I used the
self before but blindly. Which goes against my #1 Rule "Never Copy/Paste Ever".
This answer from StackOverflow helped me understand this concept a bit better.
The Simpsons, Self and Python
If you have a
list with a method called
>>> simpsons = list() >>> simpsons.append('homer') >>> simpsons ['homer']
The method is defined as this:
def append(self, arg1, arg2): # do something
simpsons object is an instance of the class
As the solution says, but using my example:
simpsons.append('homer') is called, Python internally converts this to:
If I run this again with another argument:
>>> list.append(simpsons, 'bart') >>> simpsons ['homer', 'bart']
Which means that these 2 do the same:
>>> simpsons.append('bart') >>> list.append(simpsons, 'bart')
Looking at this again:
def append(self, arg1)
self variable refers to the object.
Rock bands, Self and Python
This is another good answer about
self and Python in Quora.
The first few paragraphs create a good context for understanding
- A class has methods
- A class can have multiple objects
Here is an interesting question.
"When an object calls a method of the class, how would the method know which object has called it?"
class myBand: def __init__(self): self.instruments =  self.instruments.append('drums') def append(self): awesome append code muse = myBand() muse.append('bass') # prints ['drums', 'bass'] radiohead = myBand() radiohead.append('moog') # prints ['drums', 'moog']
Following the same example as the Simpsons
>>> muse.append('bass') >>> myBand.append(muse, 'bass') >>> myBand.append(self, arg1)