Introduction to Object-Oriented Programming(OOPS)

Now, we will see the object-oriented programming in Python. First will look into the basics revolve around the oops.

So, when someone is talking about oops then directly one thing comes into the mind there will be classes and objects. Some methods and variables associated with those objects. Let’s dig into the detail of what is class, object actually?

Let’s take the example of a company. In a company, we have employees of different profiles like Data Scientist, Data Analyst, Software Developer, Human Resource managers etc.

So, in that case, the company name is our class and objects will be different profiles employee and methods and function will be like task() – which describe their work, timings() – what is the office hours for each profile people. This is an example which may give you an overview of classes and objects and methods associated with those classes.

Now, we will talk in terms of Python like how to create a class in Python and its constructor function etc.

In Python, classes are created using “class” keyword and variables are the attributes. In class, we have a self keyword which actually works as an object and it helps in accessing the class variables and some other things will be clear to you through example.

1. Creating simple class

Let’s see how to create a class and class variable. How to access a class variable?

 

In [17]:
## creating a class name "company"
class company:
    
    ## Class variable
    welcome_msg = 'Welcome to Robofied!'
    
    ## This function is returning the working hours of employee
    def office_hours(self):
        
        ## Accessing class variable inside the function using "self"
        print(self.welcome_msg)
        
        ## variables inside the function only, same for all objects
        start_time = '10:00'
        finish_time = '18:00'
        
        return (int(finish_time.split(':')[0]) - int(start_time.split(':')[0]))
      
In [18]:
obj = company()
## Accessing the class variable using created object
obj.welcome_msg
Out[18]:
'Welcome to Robofied!'
In [19]:
obj.office_hours()
Out[19]:
Welcome to Robofied!
8

2. Instance valriable and how its value cahnge for different objects.

In this section, we will check how to create an instance variable and using self keyword how to change its value whenever we are creating a new object.

 

In [38]:
class company:
    
    ## Class variable
    welcome_msg = 'Welcome to Robofied!'
    
    def office_hours(self,start_time,finish_time):
        
        ## How to access class variable inside function
        print(self.welcome_msg)
        
        ## variables inside the function only
        ## instance variable, but differnt for different objects.
        self.start_time = start_time
        self.finish_time = finish_time
        
        print('Your office hours will be ' + str((int(self.finish_time.split(':')[0]) - int(self.start_time.split(':')[0])))+ 
              ' hours.')
In [39]:
data_scientist = company()
data_scientist.welcome_msg
Out[39]:
'Welcome to Robofied!'
In [40]:
data_scientist.office_hours('10:00','17:00')
Out[40]:
Welcome to Robofied!
Your office hours will be 7 hours.

3. __init__() function – Constructor and self keyword

In this section, we will see the __init__() function which is basically a constructor function. A constructor function is called everytime whenever we are creating an object of that class.

 

In [46]:
class company:
    
    ## Class variable
    welcome_msg = 'Welcome to Robofied!'
    
    ## Constructor function called while creating object.
    def __init__(self,Profile):
        
        self.Profile = Profile
        print('Welcome to {} team!'.format(self.Profile))

    ## User defined function for calculting hours
    def office_hours(self,start_time,finish_time):
        
        ## How to access class variable inside function
        
        print(self.welcome_msg)
        
        ## We can access variable passed to constructor function here as well.
        
        """This is because that variable is associated with object and self is basically for your understanding 
        we can say it represents object"""
        
        print('Welcome to {} team!'.format(self.Profile))
        
        ## variables inside the function only
        ## instance variable
        self.start_time = start_time
        self.finish_time = finish_time
        
        print('Your office hours will be ' + str((int(self.finish_time.split(':')[0]) - int(self.start_time.split(':')[0])))+ 
              ' hours.')
In [47]:
full_stack = company('Full stack developer')
Welcome to Full stack developer team!
In [49]:
full_stack.office_hours('9:00','18:00')
Welcome to Robofied!
Welcome to Full stack developer team!
Your office hours will be 9 hours.

I hope this coding section gives you clear idea about class variable, instance variable, __init__ function, self keyword and how use class variable inside the defined function etc.

Hope you are enjoying learning intermediate Python with us. Stay tuned!

Keep learning with us.

Leave a Comment

Your email address will not be published. Required fields are marked *