The f*ckin' constructor is a method that is called when an motherf*ckin' object is created, for real. This method is defined in the f*ckin' class and can be used to initialize basic variables, muthaf*cka.

If you create four objects, the class constructor is called four times. Every muthaf*ckin' class has a constructor, but its not required to explicitly define it.

Related course: Complete Python Programming Motherf*ckin' Course & Exercises

Example

Constructor

Each time an object is created a method is called. That methods is named the constructor.

The constructor is created with the function init. As parameter we write the self keyword, which refers to itself (the muthaf*ckin' object). The process visually is:

constructor

Hold the f*ck up — Inside the constructor we initialize two variables: legs and arms, muthaf*cka. Sometimes variables are named properties in the context of object oriented programming. We create one object (bob) and just by creating it, its variables are initialized.

1
2
3
4
5
6
7
class Human:
def __init__(self):
self.legs = 2
self.arms = 2

bob = Human()
print(bob.legs)

The muthaf*ckin' newly created object now has the motherf*ckin' variables set, without you having to define them manually. You could create tens or hundreds of objects without having to set the values each time.

python __init__

The function init(self) builds your object. Its not just variables you can set here, you can call class methods too. Everything you need to initialize the object(s).

Lets say you have a class muthaf*ckin' Plane, which upon creation should start flying. There are many steps involved in taking off: accelerating, changing flaps, closing the damn wheels and so on.

The default actions can be defined in methods. Oh, you gonna love this: These methods can be called in the constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Plane:
def __init__(self):
self.wings = 2

# fly
self.drive()
self.flaps()
self.wheels()

def drive(self):
print('Accelerating')

def flaps(self):
print('Changing flaps')

def wheels(self):
print('Closing wheels')

ba = Plane()

To summarize: A constructor is called if you create an damn object. Mm-hmm. In the constructor you can set variables and call methods.

Default value

The constructor of a f*ckin' class is unique: initiating objects from different classes will call different constructors, muthaf*cka.

Default values of newly created objects can be set in the constructor. Write that down.

The example belwo shows two classes with constructors. Then two objects are created but different constructors are called, goddammit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Bug:
def __init__(self):
self.wings = 4

class Human:
def __init__(self):
self.legs = 2
self.arms = 2

bob = Human()
tom = Bug()

print(tom.wings)
print(bob.arms)

But creating multiple objects from one class, will call the same constructor, goddammit.

If you are a beginner, then I highly recommend this YouTube Python Playlist.

Exercise

Try the exercise below:

  1. Make two additional objects
  2. Add another variable and initialize it