Object-Oriented Programming in Python (Classes and Objects)

Object-Oriented programming is a way of grouping part of your program that perform similar functions or have related properties into objects that can interact with one another.
Just simply put, OOP provides a means of structuring your program so that each properties and behaviors are in their individual objects. OOP is based on the concept of “objects” which contains the object’s attributes (data fields or properties) and the object’s behavior (methods). An object is an entity of a real world object that can be distinctly identified, like table and bird, sometimes object represent more abstract entities like opening a program, conversion of currencies and so on. For example, lets say we have a Bird object, and we assume all birds fly. Birds have different properties like name, beak color, beak size and feather color, they also possess different behaviors like flying and walking. If we are to create a Bird object, we would define its properties as its attributes and it’s behavior as methods.
Classes
A class is a template or blueprint that defines what an object data field and methods would be. An object is an instance of a class. A class usually just define the field for an object it doesn’t actually contain data. Like the example above, a Bird class would have a field for storing the properties like name, feather color and beak-size without defining the name, feather color or beak-size of the bird. To have a real Bird we would need to create an instance (an object) of the Bird class with the values for name, feather color and beak-size.
Enough talking, Let’s Practice!
Now let’s actually see what it is like to create an object in python. For this example we would be creating the Bird class and an Eagle object (an instance of the bird class). The Bird class:
class Bird():
pass
As you can see the Bird class is pretty much empty, now let create fields for the name of the bird, the beak size, beak color and feather color. To define the properties of the Bird, we need to define them in the __init__()
method, a special type of method that is called anytime you create a new object and it allows the class to initialize its attributes.
class Bird(): """ Blueprint for a Bird """ def __init__(self, name, feather_color, beak_size, beak_color):
self.name = name
self.feather_color = feather_color
self.beak_size = beak_size
self.beak_color = beak_color
Notice the self
keyword? self
is just a representation of the object itself and it must be the first parameter for all the methods in a class because anytime we call a method the object automatically passes as the first argument along with other arguments.
Now we can create the birds behavior.
class Bird(): """ Blueprint for a Bird """ def __init__(self, name, feather_color, beak_size, beak_color):
self.name = name
self.feather_color = feather_color
self.beak_size = beak_size
self.beak_color = beak_color def fly(self):
print("Flying") def walk(self):
print("Walking")
We just created a simple bird class which can be used to create any bird of our choice.
The Eagle object

As you can see in the image above the Eagle has a black and white colored feather and a yellow beak, I would just use a random value for the beak size.
To create an object in python all you need to do is just instantiate the object like this thisObject = ThisClass()
. Let create the eagle in python.
class Bird(): """ Blueprint for a Bird """ def __init__(self, name, feather_color, beak_size, beak_color):
self.name = name
self.feather_color = feather_color
self.beak_size = beak_size
self.beak_color = beak_color def fly(self):
print("Flying") def walk(self):
print("Walking")myBird = Bird('Eagle', 'Black and White', 3, 'Yellow')
myBird.fly()
As shown above we just created an Eagle, called it fly
method. To print the data of the object we use print(thisObject.what_to_print)
.
# Print bird name
print(myBird.name)# Print bird feather color
print(myBird.feather_color)# Print bird beak size
print(myBird.beak_size)#print bird state
print(myBird.fly)
Output
Eagle
Black and White
3
Flying
Conclusion
This was a brief introduction to Classes and Objects in Python. I hope you have found this useful.