Feb-07-2020, 01:26 PM
Hi community,
I understand that we need to use the "self" keyword everywhere in Python methods.
Still I am not conceptually clear what it means. I would be grateful if you can tell me in your words, why Program2 doesn't work, while Program1 does.
I believe it is important to understand the concept rather than just knowing the rule. Why is the "self" keyword important in Program2? Shouldn't the code search for the method in the Class itself, and search for it in the Superclass if not found?
I understand that we need to use the "self" keyword everywhere in Python methods.
Still I am not conceptually clear what it means. I would be grateful if you can tell me in your words, why Program2 doesn't work, while Program1 does.
I believe it is important to understand the concept rather than just knowing the rule. Why is the "self" keyword important in Program2? Shouldn't the code search for the method in the Class itself, and search for it in the Superclass if not found?
##Program1 class giveIDCard: def issueIDCard(self): self.prepareIDCard() class oldEmployees(giveIDCard): def prepareIDCard(self): self.renewIDCard() def renewIDCard(self): print('renew ID card')
Output:>>> s=oldEmployees()
>>> s.issueIDCard()
renew ID card
##Program2 class giveIDCard2: def issueIDCard2(): prepareIDCard2() class oldEmployees2(giveIDCard2): def prepareIDCard2(): renewIDCard2() def renewIDCard2(): print('renew ID card')
Error:>>> s=oldEmployees2()
>>> s.issueIDCard2()
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
s.issueIDCard2()
TypeError: issueIDCard2() takes 0 positional arguments but 1 was given