-1

I am currently studying Python and I have seen in many source codes that uses init(self) but I do not know about it.

I'm a newbie so please explain it easily.

0

1 Answer 1

0

self is like the 'this' keyword in JavaScript. If you have

def __init__(self):
    self.foo = 'bar'

that object will now have a .foo that is equal to a string of bar.

Passing self in elsewhere is used too. If you want your object to have a method

def myMethod(self, parameter1, parameter2):
    #do stuff

self is needed so that the method is attached to the created objects

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much, but I do not know what method means. Is it something similar to functions?
It's a function attached to an object. I'm guessing that you saw the code inside of a class. Every object that is created from that class, will have those attached methods that you create. I wasn't sure if you'd also seen self used there, but you'll come across that too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.