-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.

K-C-J
  • 1
  • 3

1 Answers1

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

carterw485
  • 798
  • 1
  • 7
  • 13
  • Thanks so much, but I do not know what method means. Is it something similar to functions? – K-C-J Aug 12 '15 at 09:08
  • 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. – carterw485 Aug 12 '15 at 09:15