Select Page
Software Testing

Python Oops Quick Start Guide

Python oops concept is a vast topic. However, we wanted to keep this blog post precise and act as a quick start material to learn/recap Python Oops.

python oops
Listen to this blog

We all are pretty much aware that Python is an object oriented programming language. A software tester should possess profound Programming Language skills and expert level Automation Tool knowledge before commencing scripting. Automation and coding patterns can be achieved when you are familiar with Oops concepts. For many automation testers, Oops concept is still a grey area. However, to setup test automation framework & excel in automation testing career, Object Oriented Programming Concepts are important. We, as a QA company, has a concrete career journey from automation tester to Test Automation Architect role. If you are familiar with Python Oops concept, you can create test automation libraries which are useful for your team to ease their automation scripting effort. This blog will provide you a sneak peek review of Python oops concepts.

Class Declaration

You can define class variable, instance variable, and methods in a class. Below snippet shows how to define a class.

class SuperClass():
    pass

Class with Constructor

Constructor is a special method which will be invoked when an instance is created. When Line #7 is executed, it will invoke ‘__init__’ method. i.e. constructor method. You may ask a question – Why are we passing ‘self’ as an argument in constructor method? Answer: ‘self’ represents the instance of the class.

#Class Definition
class SuperClass():
    def __init__(self):
        print("Constructor is called")

#Object Creation
obj = SuperClass()

Class Variable

Class variable which is stored in Class memory not in instance memory.

# Class Definition
class SuperClass():
    counter = 0 #Class Variable
    def __init__(self):
        print("Constructor is called")

# Object Creation
obj1 = SuperClass()
obj2 = SuperClass()

obj1.counter +=1
obj1.counter +=1

print(obj1.counter)
print(obj2.counter)

In the above code, Line #14 prints 2 and Line #15 prints 0. However, as per the class variable paradigm it should print 2 for both the instances. The reason is line #11 & #12 change the values using its instances. To change class variable value, you need to use class name not instance. Please refer the below code. It changes class variable’s value using Class Name.

# Object Creation
obj1 = SuperClass()
obj2 = SuperClass()

SuperClass.counter +=1
SuperClass.counter +=1

print(obj1.counter)
print(obj2.counter)

Instance Variable

Instance variables are stored in Instance memory not in Class memory. Let’s see how to declare and use Instance variable.

# Class Definition
class SuperClass():
    def __init__(self, name, age):
        self.name = name
        self.age = age
        print("Constructor is called")

# Object Creation
obj1 = SuperClass("John", 40)
obj2 = SuperClass("Peter", 50)

In the above snippet, ‘name’ and ‘age’ are instance variables. When you create instance for SuperClass, memory will be allocated for ‘name’ & ‘age’ variables in instance memory.

Inheritance

#Super Class
class Animal():
    def __init__(self, name, age):
        self.name = name
        self.age = age

#Sub class
class Cat(Animal):
    def __init__(self, name, age):
        super(Cat, self).__init__(name, age)

instance_1 = Cat("Kona", 2)

print(instance_1.name)

In Conclusion

Python oops concept is a vast topic. However, we wanted to keep this blog post precise and act as a quick start material to learn/recap Python Oops. As a test automation company we create useful test automation libraries with coding patterns and best practices. When you are aware of code design patterns and follow best practices, you can deliver quality test automation scripts with ease.

Comments(0)

Submit a Comment

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

Talk to our Experts

Amazing clients who
trust us


poloatto
ABB
polaris
ooredo
stryker
mobility