Object Oriented Programming

Till now , we have been dealing with very small codebases however as you start to deal with much larger Python scripts , it becomes difficult to organize the code by just using the functions. This is where a concept called object oriented programming is used. From here on we will just call it 'OOP'.


OOP allows programmers to create their own objects that has custom methods and attributes associated with them. This really keeps the codebase organized and very scalable.


In OOP , a class is used as an template to create an object and store the methods and atributes associated with it. Once these methods are created within a class , they can easily be called later on.


Let us look at all the components of a very basic class. Do not worry if it all looks a bit confusing for now.


components of a basic python class.
'self' is a very special keyword . It allows the attributes to be linked between the methods within a class. Once the attribute is linked to self in the '__init__' method then that attribute can easily be accesed by other methods ( within the class).
Creating a class

Let us create a simple class where we use car as our object. What do you think should be attributes of this class?? well attributes are nothing but characteristics of the object. So in this case we consider the company of the car and the color of the car to be its attributes.


So our class looks something like this:

class Car():
  def __init__ (self,param1,param2):
    self.company = param1
    self.color = param2
param1 and param2 ( you can change their name to whatever you want )are the values that are passed into the class when we create an instance of that class. These values are then assigned to the attributes of the object.
Although there is no restriction on the name of the parameters , most of times you'll see that the name of the parameter is same as the name of the attribute. So in the class above , we could simply rename param1 and param2 to company and color. So the code looks something like this:
class Car():
  def __init__ (self,company,color):
    self.company = company
    self.color = color

Now that we have our class ready , Let us create an instance of the class where we pass in the values for attributes and then we can call our class to get the value for any attribute. An instance of the class looks something like this:

#creating an instance of the class
my_car = Car('bmw','black')

Now let us combine the code and call our class to get values for attributes.

class Car():
  def __init__ (self,company,color):
    self.company = company
    self.color = color

#creating an instance of the class
my_car = Car('bmw','black')

#getting values of attributes
print(my_car.company)
print(my_car.color)
    
output:
bmw
black
Class object attribute

Say we have an object of elephant and we want our class to contain some very basic and very obvious attributes about elephants with pre-defined values associated with them. The class object attribute allows us do just that. The attribute must be defined before the __init__ method and is accesible by the instance of class like any other attribute. In the code given below , species is our class object attribute.

class elephant():
  
  #class object attribute is placed above __init__ method
  species = 'mammal' 
  def __init__(self):
    #some code
    pass
  
my_elephant = elephant()
print(my_elephant.species)
    
output:
mammal




Popular Tutorials

Creative Commons Licence
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.