← Back to libraryQuestion 198 of 468
🐍PythonIntermediate

OOP in Python — Classes, self, and Inheritance

📌 Definition:

Python supports object-oriented programming with classes, instances, the self parameter, __init__ constructors, and inheritance. Page Object Model frameworks are built on these.

📖 Detailed Explanation:

class LoginPage: def __init__(self, driver): self.driver = driver defines a class; self refers to the instance and must be the first parameter of instance methods. __init__ initializes attributes. Inheritance (class LoginPage(BasePage)) reuses/extends a parent, and super().__init__() calls the parent constructor. Methods can be overridden. Testers use OOP for Page Objects (each page a class with locators + actions), base classes for shared setup, and mixins for reusable behavior. Class vs instance attributes matter: class attributes are shared across instances.

🔑 Key Points:
  • self is the instance; first param of instance methods
  • __init__ initializes instance attributes
  • Inheritance + super() reuse/extend base classes
  • Foundation of Page Object Model frameworks
🌍 Real-World Example:

A Page Object: class LoginPage(BasePage): def login(self, user, pw): self.type(self.USER, user); self.type(self.PASS, pw); self.click(self.SUBMIT) — encapsulating the login page's locators and actions in one class.

🎯 Scenario-Based Interview Question:

A candidate defines a class attribute results = [] and finds every Page Object instance shares the same list. Why?