Explain classes, __init__ and self in Python
So the first thing you need to know about classes is the difference between a class and an instance of a class. A class is like a blueprint, it tells you about some thing you want to make. An instance is the thing that gets made.
For example, if you write up a blueprint for an airplane, the blueprint is like when you define a class. The airplanes that get made from that blueprint are like instances of a class.
Defining a class looks like this:
class Airplane:
pass
(Normally you would have some more code instead of
pass. I'll explain that later.)
Now once you define a class you can create instances of a class like this,
Airplane(). For example,airplane1 = Airplane()
airplane2 = Airplane()
Here we created two instances of the Airplane class and put them in the variables
airplane1 and airplane2. The important thing here is that you can change airplane1 without affecting airplane2. They're two separate instances.
Okay now let's go back and talk about what goes inside a class. Let's take our Airplane class and fill it out:
class Airplane:
def __init__(self):
print "A new instance got made!"
So what's going on here?
__init__ is a function that gets run when you create an instance. That's it! So if you go back to where we created the two Airplane instances,airplane1 = Airplane()
airplane2 = Airplane()
...what would happen is, "A new instance got made!" would be printed out twice.
What about the
self parameter? I think this would be easier to understand if we added a new method.class Airplane:
def __init__(self):
print "A new instance got made!"
def fly(self):
print "I'm flying!"
So if you wanted to call this method you'd do something like this:
airplane1.fly(). Actually this is the same thing as this: Airplane.fly(airplane1). Both of these would do the same thing, i.e. print out "I'm flying!". So airplane1 is the instance that we used to call our fly method. This instance is what gets passed to self.
Okay so lets say you want to make airplanes that know where they are by keeping an x and y coordinate. You could do this:
class Airplane:
def __init__(self, x, y):
self.x = x
self.y = y
This will make it so that an instance of Airplane will each have their own variables x and y. You can make an instance of airplane like this:
airplane1 = Airplane(0, 0)
airplane2 = Airplane(3, 4)
This will do something like the following:
Airplane.__init__(airplane1, 0, 0)
Airplane.__init__(airplane2, 3, 4)
Which is going to set
airplane1.x to 0, airplane1.y to 0 and airplane2.x to 3, airplane2.y to 4. You could try printing it out and you'd get something like this:>>> print airplane2.x
3
>>> print airplane2.y
4
Then maybe you could create a method that finds the distance between two planes:
import math class Airplane: def __init__(self, x, y): self.x = x self.y = y def distance(self, another_plane): diff_x = self.x - another_plane.x diff_y = self.y - another_plane.y distance_total = math.sqrt(diff_x**2 + diff_y**2) return distance_total>>> class Boat(object): ... pass ... >>> b = Boat() >>> b <__main__.Boat object at 0x10046c950> >>> type(b) <class '__main__.Boat'>So a class is a thing that makes objects of that class. ABoatis just a template that makes things that areBoats. Why__init__? Well an object is something that combines state and functionality. An object's state is represented by its member variables, while its functionality is performed by its methods (the 'smaller functions' it contains).__init__is a method that is called for us automatically after an object is created in order to initialize its internal state (set its member variables) to something that makes sense.Whyself? When you define methods inside of a class, these are methods that are going to be operating on objects made from that class. Inside these methods,selfrefers to the current object, so you can reference member variables or other methods of the object in order to do more stuff. Remember that methods execute on objects of a class, not the class itself, and there can potentially be many objects of the same class, so we need some way to reference the current object in each method. In Python we use an explicitselfreference as the first parameter to each method to accomplish this.Hope that helps.
Comments
Post a Comment