Quantcast
Channel: What is the purpose of the `self` parameter? Why is it needed? - Stack Overflow
Viewing all articles
Browse latest Browse all 77

Answer by saran for What is the purpose of the `self` parameter? Why is it needed?

$
0
0

my little 2 cents

In this class Person, we defined out init method with the self and interesting thing to notice here is the memory location of both the self and instance variable p is same <__main__.Person object at 0x106a78fd0>

class Person:    def __init__(self, name, age):        self.name = name         self.age = age     def say_hi(self):        print("the self is at:", self)        print((f"hey there, my name is {self.name} and I am {self.age} years old"))    def say_bye(self):        print("the self is at:", self)        print(f"good to see you {self.name}")p = Person("john", 78)print("the p is at",p)p.say_hi()  p.say_bye() 

so as explained in above, both self and instance variable are same object.


Viewing all articles
Browse latest Browse all 77

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>