Python Object Oriented Programming
As we all know Python is Object Oriented Programming language. But you won’t believe we are using Python OOP from our first program itself. For example if you try following snippet you will get what I am saying.
x = 2
print(type(x))
It will give you following result: <class 'int'>
You can see this in any data item like list, tuple, dictionary sets …etc. That is each datatype is belongs to a class. For example append is a method within the class named list. We will discuss what is method in this post later.
How do we create simple class?
Using class keyword we can create new class. The syntax of class is given below:
class class_name:
method_definitions
Let us create our own class,
class Employee:
pass
Let us create a new objects of our class Employee:
employee1 = Employee()
employee2 = Employee()
Let us check whether we are created instances of class Employee successfully:
print(type(employee1))
This will give you something like <class '__main__.Employee'>
. Then it is perfect.
Now it is the time to create instance variables. Instance variables are used for attach data to individual objects. Using these instance variable we can attach data field directly to at runtime. Instances are written outside class.
employee1.firstName = 'Arun'
employee1.lastName = 'Soman'
employee1.email = employee1.firstName + employee1.lastName+'@company.com'
employee2.firstName = 'Anuja'
employee2.lastName = 'Soman'
employee2.email = employee2.firstName + employee2.lastName+'@company.com'
print(employee1.email)
print(employee2.email)
Our primary concern is to reduce number of lines in the program. So it is not a good way to write program. In next post we will discuss about self obsession and how to reduce number of lines in the program using Python default constructor called init .
Writing Methods Inside Class
Writing a method is how we give objects of a class the ability to respond to messages; another way of saying this is that methods give objects of a class behavior.
Python supports following types of methods
- Regular Methods
- Class Methods and
- Static Methods
We will discuss about all of the above methods in upcoming blog posts. Let us create a regular method by extending our previous program.
class Employee:
def fullName(firstName, lastName):
return firstName + ' '+ lastName
# instances or objects
employee1 = Employee()
employee2 = Employee()
# instance variables of employee1
employee1.firstName = 'Arun'
employee1.lastName = 'Soman'
# instance variables of employee2
employee2.firstName = 'Anuja'
employee2.lastName = 'Soman'
# calling method in the class
print(Employee.fullName(employee1.firstName,employee1.lastName))
# calling method in the class
print(Employee.fullName(employee2.firstName,employee2.lastName))
In the program given above, we created an Employee class. In the main program we can see instances of Employee class. Using those instances we created new instance variables called firstname and lastname. Next we wrote a method called fullname in the class in order to concatenate firstname and lastname. In the last two print statement the method fullname is called. As I mentioned earlier, we cooked up this program by writing instance variable over and over again which is not a good idea. How to eradicate this problem? See our next post for solution.
Reference: