0

What is "self" used for?

I see it almost everywhere in python codes, in situations like "self.something" or "something(self)" It is something involving the scope of the variables?

7th Oct 2016, 7:06 PM
Daniel de Lizaur
3 Respostas
+ 1
It's the currently instantiated object. The difference is not completely explained in the course (nor here) but basically comes down to: In Python you can execute code inside a class without creating an object for it; it's a static copy (and 'self' is nonsense). Once you create an object you can have copies; each copy needs to know to reference its own state and nobody else's.
7th Oct 2016, 7:16 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
Self represents the instance of any class. Actually self is just a word. You can write anything instead of "self". The important thing is the first parameter of init method always represents the instance. For example these are same things: def __init__(self) self.attribute def __init__(anyword) anyword.atrribute
7th Oct 2016, 7:45 PM
Koray
0
self means it will pass the instance of a class as a argument to the function where it is being called using self
7th Oct 2016, 7:28 PM
Waseem
Waseem - avatar