Self, the instance
- VC Healy
- Jun 15, 2020
- 1 min read
For a long time I never understood why I was placing self into functions.
I did it because it worked but I just never understood why. I knew the function would give me instances and I understood what an instance was.
During my deep diving in Python something sparked that I really need to understand why I was adding in self to a function. It wasn't until I was creating a simple employee function that built a list of employees that it sank in.
Forget all the detailed information of what is required for an employee record and focusing on just one attribute, first_name, the example function is
def employee(self, first_name):
self.first_name = first_name
Adding employees.
employee1 = employee(Jim)
employee2 = employee(John)
'employee1' is the self attribute and 'Jim' the first_name
'employee2' is the self attribute and 'John' the first_name
self is used the attribute for the instance symbol.
Comments