Python Oops Quick Start Guide

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 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 Variable

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

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.

Instance Variable

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

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.


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.

Leave a Reply

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