So Python has many "magic" methods, marked by double underscores surrounding the name. (for this reason, they're also sometimes called "dunder" methods.) Rake Ketter's guide on the subject is the best you'll find, it's very clear. We're just going to address __init__
here.
So __init__
is basically a method that gets called when you create a new "instance" of a class. Let's examine this with dogs...
# So first we're going to create a new "Dog" object. A dog knows it's name and how to bark.
class Dog(object):
def __init__(self, name, woof): # __init__ takes a couple variables, and `self`...
self.name = name # and assigns them as properties of "self".