Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I feel like I was in the same boat as you a few weeks ago, and although very shakey on them still, I'm going to try to explain self and init to you as best I can. Please be gentle, other commentors! ;)</p> <p>I'm going to try to explain "self" first because it might make init more clear. When I want to make a class, I can do so by:</p> <pre><code>class Car: # I can set attributes here ( maybe I want all my cars to have a: size = 0 # well eventually my car object is going to need to do stuff, so I need methods - # methods are like functions but they are only usable by the class that encompasses them! # I create them the same way, just inside the indentation of the class, using def func(): def drive(self): print "vroom" # pretend this is the function that I would call to make the car drive. </code></pre> <p>Okay. We have some stuff to talk about with just this layout here. We've DEFINED what a car should do, but we havent made anything yet. (i promise this is all relevant!) In order to make an INSTANCE (a single occurrence) of car, we can assign the car object to a new variable:</p> <pre><code>myCar = car() </code></pre> <p>now I can use all the METHODS we defined in the car class - like drive! we would call that function by typing in:</p> <pre><code>myCar.drive() </code></pre> <p>this would print "vroom" I can make a second INSTANCE of the car() class in the same program (it would be a totally different object though) by doing:</p> <pre><code>newCar = car() </code></pre> <p>Now, here comes the inception part... I've made a really simple class, and classes get really big and scary and it's literally impossible to understand them all in one night, but I'm going to explain self to you now.</p> <p>"myCar", the variable I used to hold the car object I made, becomes the "self" argument if I have other methods that need to refer back to that object. in essence, myCar.vroom() is the same as saying self.vroom(), if I needed to refer to .vroom() in ANOTHER method for the class.</p> <p>wrapping it up we have something that looks like this:</p> <pre><code>class Car: size = 0 # a global attribute for all car objects def drive(self): #self is the argument! print "vroom!" myCar = car() # we've initialized the class but havent used it here yet! myCar.drive() # this prints "vroom" </code></pre> <p>another way to think of this would be saying the argument, just like in a normal function, is self is just a placeholder for whichever object is calling the function.</p> <p>Now, if that made sense awesome, if not I'll edit it again. def <strong>init</strong>(self): is using the same theory, of taking a single object you made from the class, and just feeding it instructions to do every time the class creates an object.</p> <pre><code>class car: def __init__(self): # needs self, to refer to itself, right? self.name = name # now we can assign unique variables to each INSTANCE of the car class. </code></pre> <p>you can take def <strong>init</strong> and do some crazy stuff with it, like inside it you can immediately call other methods and stuff. basically, its like saying 'Hey object! You are alive, go check what is inside the <strong>init</strong> function, you need to have all that stuff! GO!</p> <p>Let me know if this helps, or if I can make anything more clear. like I said, I barely just wrapped my head around all this, so maybe my explanation needs some work. Cheers :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload