Object Oriented Programming in Python Part-1

Ankit Deshmukh
TechGannet
Published in
2 min readJul 1, 2018

Object Oriented Programming allows you to create your own objects. It also allows us to create code which is reusable.

type() function is used to check the type of object.

print(type(1))
print(type([]))
print(type(()))
print(type({}))
Output:
<class 'int'>
<class 'list'>
<class 'tuple'>
<class 'dict'>

Above all are the objects defined in Python i.e. bult-in.

Can we create our own object types ? Yes, we can. We can create our own objects using class .

  • What is a class ?

A class is a code template to create objects. It is a blueprint for the Object. Class is created by the keyword class .

Let’s assume we have a class animal . It contains all the details about colour,size,age etc. We are creating an empty class animal .

class Animal:
pass
  • What is an Object ?

In Python, everything is an Object. Object is anything which has attribute and behavior. Let’s see an example:

Horse is an Object.

  • name,age, colour are the attributes
  • running,eating are behavior

So, in Python, Object is an instance of a class. The example for the object of animal class can be:

horse = Animal()

Here, horse is an object of animal class.

  • Creating Class and Object:
class Animal:
def __init__(self,color,name):
self.color = color
self.name = name

dog= Animal('red','Doggy')
print("Color of {} is {}".format(dog.name,dog.color))Output:
Color of Doggy is red

Here, we have created class Animal . Then we have defined attributes also i.e. color and name . Then we have created the instance of Animal class i.e. dog . we can access the instance attributes using dog.name and dog.color .

  • What is an Attribute ?

Attribute is a characteristic of an Object. An attribute for horse may be its name,colour etc.

class Animal:
def __init__(self,color):
self.color = color

horse = Animal('Red')
dog = Animal('Black')
print(horse.color)
print(dog.color)
Output:
Red
Black

Explanation:

__init__() is called automatically when the object has been created:

def __init__(self, color):

Each attribute in a class definition begins with a reference to the instance object. The value is passed during the class instantiation. Color is an argument.

We have two instances of the Animal class. We can then access these attributes using horse.color and dog.color .

We have class attributes also. Class Object Attribute is defined outside of any methods in the class.

class Animal:
breed="indian" # class object attribute
def __init__(self,color):
self.color = color

horse = Animal('Red')
print(horse.breed)
Output:
Red
Indian
  • What are Methods ?

Methods are functions defined inside the body of a class. They are used to perform operations with the attributes of our objects.

class Animal:

# instance attributes
def __init__(self, name, age):
self.name = name
self.age = age

# instance method
def run(self):
return "{} is running".format(self.name)
def eat(self,bread):
return "{} is eating {}".format(self.name,bread)
# instantiate the object
dog = Animal("Doggy", 10)
# call our instance methods
print(dog.eat("bread"))
print(dog.run())

Output:
Doggy is eating bread
Doggy is running

In above program, we have defined two methods, run() and eat(). These methods are called on an instance object and hence they are called instance methods.

That’s it! We will look into Inheritance and Polymorphism in next chapter.

Happy Coding!

--

--