Creating and Instantiating a simple class in python
Tags: beginners, python, tutorial, oop
Published: 2017-11-29 00:48:05

---
title: Creating and Instantiating a simple class in python
published: true
description: classes in python 
tags: beginners,python,tutorial,OOP
---

Last month i started taking a course on python and i stumbled on a tutorial on object oriented programming from the python perspective, and through out this week and coming weeks i would be sharing what i have been able to learn.

Today we would be looking at class creation and instantiation. To help us understand this better we would be creating an employee class, each employee would have attributes like name,email,pay,etc. Now lets have a head first attempt at creating and instantiating a class.

![Alt text of image](https://thepracticaldev.s3.amazonaws.com/i/x5nuuarkq42lpap1qf7z.PNG)

The above code snippet shows how to create a class in python, the pass keyword under tells python to neglect the class, without this keyword, python would see the class as an empty one and return an error. A class is a blue print for creating instances and each unique employee that we create would be an instance of that class.

Example:
![Alt text of image]
(https://thepracticaldev.s3.amazonaws.com/i/mda7alqfrturbfpmutbb.PNG)

They above code shows the creation of unique instances of the Employee class and to assign data to this instances we would be using instance variables. Instance variables contain data that is unique to the instance alone.  To create instance variables for each employee we would do the following:

![Alt text of image]
(https://thepracticaldev.s3.amazonaws.com/i/8nsraz5i80musomqh5ts.PNG)

Imagine a situation we have 1000 employees to create, trying to set the variables each time we want to create an employee would mean having a-lot of code and errors. But python allows you to be able to create instances automatically and this we could do using its **(__init__)** method. 

![Alt text of image]
(https://thepracticaldev.s3.amazonaws.com/i/odirggaagoo712d8vne2.PNG)

the init method after creation receives an instance automatically called (**self**), and we also pass in other attributes of the Employee class like name and pay. Next stop we would be looking at setting the instance.

![Alt text of image]
(https://thepracticaldev.s3.amazonaws.com/i/smglq4jknr6pjzsn4i8x.PNG)
with that done, when we create our employee, the instance would be passed in automatically, all we would bother ourselves with would be to provide the employee attributes.
![Alt text of image]
(https://thepracticaldev.s3.amazonaws.com/i/k4mfjlh2yyetqrq10t6k.PNG)
on running this script **(emp_1)** would be passed into the **(init)** method as self.
Now suppose we want to print each users fullname, we could actually use the format function in python to concatenate strings to get the fullname, but this would require doing this every time we want to print an employees fullname, but to better enjoy the advantage of code re-use, we would be creating a method to help us generate each employees fullname.
![Alt fullname]
(https://thepracticaldev.s3.amazonaws.com/i/u942mb84exp728aijfv3.PNG)

That is it for class and instance of a class creation. Below is the full source code for the piece. 
![Alt text of image]
(https://thepracticaldev.s3.amazonaws.com/i/6ewfxo89ssb8pfm3rqty.PNG)

**(Note)**
while trying to print employees fullname, you could either do this--> **(emp_1.fullname())** or you could try **(Employee.fullname(emp_1))**, they both mean the-same thing, in-fact the later is what happens in the background when you run  the program.

Tomorrow we would be looking at the difference between instance variables and class variables, the difference between regular,static and instance methods