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 Rishi for What is the purpose of the `self` parameter? Why is it needed?

$
0
0

I would say for Python at least, the self parameter can be thought of as a placeholder.Take a look at this:

class Person:  def __init__(self, name, age):    self.name = name    self.age = agep1 = Person("John", 36)print(p1.name)print(p1.age)

Self in this case and a lot of others was used as a method to say store the name value. However, after that, we use the p1 to assign it to the class we're using. Then when we print it we use the same p1 keyword.

Hope this helps for Python!


Viewing all articles
Browse latest Browse all 77

Trending Articles