10) b. Creating a Simple Class

So how do you create your own class? It is very simple. You need to use the class built-in which allows you to create a new class.

Notice that the first letter of a class is always capitalized. You should remember this because it will help you distinguish between classes and python modules while importing later on.

Here is the simplest class with no member variables or functions. This is how you declare a class and instantiate an object. Notice that the class is a definition and when we create a variable of the class we instantiate by calling the class_name().

Now that you’ve seen how a simple class is created. We will add a few variables and functions.

Classes are like a container that binds together a bunch of variables and functions together. Take a look at the following example:

Employee David tagged as male has an age 28
Employee Shelby tagged as female has an age 35
Employee Roger tagged as lgbt has an age 28

Notice how classes bind together 3 different variables (age, name and gender) and a function into a one variable emp1. These 3 variables are called as class attributes.

Also notice that inside a class, whenever you refer to any of the class attributes, you refer to them with a self keyword. The self keyword makes it clear to Python that the variable that you’re talking about is the one that belongs to the class.

Another thing that we’re using in this class is the __init__ function. Remember when we initialize a string variable like str1 = “Hello world”? In the case of classes, whenever you create an object of the class, the __init__ function will assign all new values to all the member attributes.