Well, I am a newbie in Python and I am not able to understand the difference in using self
and this
keywords in Python.
This is the code that uses self
as parameter :
class restaurant():
bankrupt = False
def open_branch(self):
if not self.bankrupt:
print("branch open")
x=restaurant()
print(x.bankrupt)
y=restaurant()
y.bankrupt=True
print(y.bankrupt)
And this is the code that uses this
as parameter :
class restaurant():
bankrupt = False
def open_branch(this):
if not this.bankrupt:
print("branch open")
x=restaurant()
print(x.bankrupt)
y=restaurant()
y.bankrupt=True
print(y.bankrupt)
Both these approaches gave me the same output. So I m not able to understand why we use self
when this
solves our problem. Maybe my interpretation of self
is wrong. I looked a lot of internet stuff but did not found anything relevant.
Can anyone please solve my issue.