Register Login

Class and Object in Python

Updated May 20, 2018

Why should we use classes in Python?

Now just specific to python. You can see classes being used in most modern programming languages and It enable us to logically group data & functions in a way so it becomes easy to reuse & also easy to build upon if needed.

We are going to represent our employees in a Python carve. This is a great use for a class because each individual employee is going to have specific attribute and methods. For example, each employee will have a name, an address, a pay and also the actions that they can perform. So, it will be easy if we have a class which can be used as a blueprint to create each employee so that we don’t have to do it manually each time from the scratch.

Create Empty Class

Create a simple employee, empty class. To create a class it’s just as easy as a ‘Class Employee’:

Now  leave the employee class empty for now

#Python Object-Oriented Programing

class Employee:

pass

Note: if you have a class or function that you wanna leave empty for the time being, then you can simply put in a pass statement & Python will know that you just want to skip that for now.

Create Instance Variables 

A class is a basically blueprint to create instances, and each unique employee that we create in our class will be instances of that class. So for example, if we say, Emp_1 = employee and Emp_2 = employee, then each of these are going to be unique instances of an employee class. Instance variables contain data that is unique to each instance.

#Python Object-Oriented Programing

class Employee:

emp_1 = Employee()

emp_2 = Employee()

print(emp_1)
print(emp_2)

emp_1.first = 'Ram'
emp_1.last = 'Kumar'
emp_1.email = 'ram@ram.com'
emp_1.pay = 10000

emp_1.first = 'Test'
emp_1.last = 'Kumar'
emp_1.email = 'test@test.com'
emp_1.pay = 50000

print(emp_1.email)
print(emp_2.email)

Create init () Method

Now if we want all of the information for all of the employees when they are created rather than doing all of these manually like we did above as setting these variables manually every we are gonna use a special  ‘init’ method. You can use this method as initializer.

After creating our instances employee class right here, we can pass in the value that we specify in our new method. In our new method paste the instance that we call ‘self', the 1st name, last name & pay as the arguments.

#Python Object-Oriented Programing

class Employee:
def __init__(self, first, last, pay):
self.first=first
self.last=last
self.pay=pay
self.email=first+'.'+last+'@company.com'

emp_1 = Employee()
emp_2 = Employee()

print(emp_1)
print(emp_2)

emp_1.first = 'Ram'
emp_1.last = 'Kumar'
emp_1.email = 'ram@ram.com'
emp_1.pay = 10000

emp_1.first = 'Test'
emp_1.last = 'Kumar'
emp_1.email = 'test@test.com'
emp_1.pay = 50000

print(emp_1.email)
print(emp_2.email)
print('{}{}'.format(emp_1.first,emp_1))

Now if we want to perform some kind of action, such as the ability to display the full name of an employee.  We can do this manually outside the class to come down here and do ‘print’ and we can get the full name by putting in two places over there and doing a ‘format’ and saying empl_1.1st and emp2_1.last and then print this out but that’s like each time you wanna display the employee's full name, so instead that create a method within our class that allows us to put this functionality in one place.

So within our class we are going to create a method called ‘full name’ and call that self. & instance is the only argument that we need in order to get the full name.

#Python Object-Oriented Programing

class Employee:
def __init__(self, first, last, pay):
self.first=first
self.last=last
self.pay=pay
self.email=first+'.'+last+'@company.com'

def fullname(self):
return '{} {}'.format(self.first,self.last)

emp_1 = Employee('user1','userlast',50000)
emp_2 = Employee('user2','user2last'6000)

emp_1_fullname()
print(Employee.fullname(emp_1)
#print(emp_2.fullname())

Note: 1 common mistake that we do while creating methods that is forgetting this 'self' argument for the instance.

When we call the method on a class, then it doesn’t know what instance we want that method to run with so we do have to pass in the instance and we get passed it as self.

Now if we go ahead and print this out and run it then you can see that it works just like if we were to print that employee1 for that format.


×