Search Here

Python - Class

As we know that python is an Object Oriented high level programming language. Just like Java Script and another programming languages python gives you the option writing code that is procedural or object oriented. For that you can use classes in python. Classes are really great way of encapsulating functionality. That can be kept together and pass around as a complete module to use in other projects.
Python - Class

How to use class in python

Classes are defined using the "class" keyword and after that keyword we will give a name to that class.

#!/usr/bin/python3

# Example of class
class example_class():
    def method_1(self):
        print("Example Class method_1")
        
    def method_2(self,some_string):
        print("Example Class method_2 " + some_string)        

def main():
    # using class methods
    obj = example_class()
    obj.method_1()
    obj.method_2("I am passing some string")
    
    
if __name__ == "__main__": main()      
 



Code Description

In above code snippet we have created a class named "example_class" with empty argument or parameters. The reason is that here our class is not based on another class, if our class is based on some other class than I put the name of that class in parenthesis. So our created class is a base class that does not depend on another class therefore I don’t have to put anything in there.


Inside class I can define functions or in the sense of Object Oriented Programming (OOP) methods that are part of this class. So in my case I have defined two functions or methods named "method_1 and method_2".

# Example of class
class example_class():
    def method_1(self):
        print("Example Class method_1")
        
    def method_2(self,some_string):
        print("Example Class method_2 " + some_string)      
 



Why we use self in functions/methods

Because class is an Object Oriented concept that's why here the first argument are "self", and the keyword self refers to the object itself. That’s hence the used the word self. So inside this method self will refer to the particular instance of this object that’s being operated on.


In main function I instantiate the class by declaring a variable named obj and just assigning the name of my class with parenthesis. By using this variable "obj" we can call functions or methods of that class. We can also call methods of any other object. To call functions or method we simply use dot "." operator.

def main():
    # using class methods
    obj = example_class()
    obj.method_1()
    obj.method_2("I am passing some string")    
    
if __name__ == "__main__": main()         
 



Here notice that when we call our second method we did not pass self as a first argument because python handles this on runtime. I just have to care about the argument that I am passing in, in this case it is a string "I am passing some string". 

Output


Python - Class


You can see that inside method_1 we are printing "Example class method_1" so the result is right here. In method_2 we are printing out "Example class method_2" and concatenating some_string (whatever string are passed in). You can see that output what is happening here.

Inheritance

Inheritance is an Object Oriented Programming (OOP) concept. It's mean that using characteristics of parent class in child class. For example we have two classes, class A and class B and using inheritance concept of OOP we can use functions, methods and variables of the parent class A in child class B.

#!/usr/bin/python3

# Example of Inheritance
class example_class():
    def method_1(self):
        print("Example Class method_1")
        
    def method_2(self,some_string):
        print("Example Class method_2 " + some_string)
        
class another_example_class(example_class):
    def method_1(self):
        example_class.method_1(self)
        print("Another Example Class method_1")
        
    def method_2(self,some_string):
        print("Another Example Class method_2")                

def main():
    # using class methods
    obj = example_class()
    obj.method_1()
    obj.method_2("I am passing some string") 
    obj1 = another_example_class()
    obj1.method_1()   
    
if __name__ == "__main__": main()    
       
 



Code Description

In above code we have two class the first one is "example_class" and the other is "another_example_class" and this class is depend on parent class named "example_class". So here we have two classes parent (example_class) and child class (another_example_class). As we have already discussed that if a class is dependent on another class than we need to pass its (parent class) name as an argument in child class. By using this I am saying that another_example_class is based on my class or "another_example_class" is inheriting from "example_class".

class another_example_class(example_class):
    def method_1(self):
        example_class.method_1(self)
        print("Another Example Class method_1")
        
    def method_2(self,some_string):
        print("Another Example Class method_2")       
 



Inside child class "another_example_class" I am defining two methods. So I am overriding the methods of "example_class".

def main():
    # using class methods
    obj = example_class()
    obj.method_1()
    obj.method_2("I am passing some string") 
    obj1 = another_example_class()
    obj1.method_1()   
    
if __name__ == "__main__": main()   
       
 



In the main function what we are doing is we are creating an instance of "another_example_class" by creating a variable obj1. And then we are calling the function (method_1) of child "another_example_class" class. The first thing in "method_1" I am doing is that I am calling the inherited method of parent class or super class named "example_class" which is the class I am inheriting from.

Output
Python - Class


So you can see that the code which we have originally is already there. And the output string "Example Class method_1" which is being called on the inherited class and then "another_example_class" method_1 is printed out.
So that's the quick introduction of OOP in python. You can define classes in python as well as inherit classes in python.

Post a Comment

0 Comments