Abstraction

Python · Functions & OOP

Theory

Abstraction means showing only what a caller needs and hiding how it is done. A PaymentMethod base class defines one method, pay(amount), and each concrete class (CreditCard, UPI) fills in the details. Code that takes payments only calls pay(); it never needs to know how a card is charged or a UPI request is sent.

The simplest way to define the interface is a base method that raises NotImplementedError, as in the example. The more formal way uses the abc module: class PaymentMethod(ABC) with @abstractmethod def pay(...). Then Python refuses to create an object of any class that has not implemented every abstract method, catching mistakes when the object is created instead of later when the method is called.

Why we use it

Abstraction reduces complexity: callers work with a small, stable interface, and you can add or swap implementations (a new wallet) without touching the code that uses them.

When to use it

Use it when several classes must offer the same operations in different ways, such as payment methods, file exporters or data sources. Skip it for a single implementation; an extra abstract layer then adds code without benefit.

How it works

  1. Checkout calls method.pay(100)
  2. Caller only knows the PaymentMethod interface
  3. Is it a CreditCard object? Yes: CreditCard.pay charges the card. No: UPI.pay sends a UPI request.
  4. Details stay hidden inside the subclass
  5. Payment done; caller code unchanged

Syntax

class Base:
    def action(self):
        raise NotImplementedError

Example

class PaymentMethod:
    def pay(self, amount):
        raise NotImplementedError

class CreditCard(PaymentMethod):
    def pay(self, amount):
        print(f'Charged {amount} to card')

CreditCard().pay(100)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic