Abstraction in Python — Python Technical Interview Questions & Answers
Abstraction in Python
Abstraction in Python
- Abstraction means show what the user needs and hide the messy inside. pay() is enough; don’t dump card-number checks into the caller. In Python, ABC can force children to implement methods: from abc import ABC, abstractmethod. A child that forgets the method fails when you instantiate it.
- Everyday abstraction is also just a clean method name. ABC is the interview pattern. One sentence: clear interface, hidden internals. Then mention abstractmethod if they push.
- Abstraction in Python — output: 12.56. Shape() would fail. Circle fills in area().
Technical Interview Tip: ABC idea in one sentence.
Code Examples for Abstraction in Python
Example
# Abstraction
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def __init__(self, r):
self.r = r
def area(self):
return 3.14 * self.r * self.r
print(Circle(2).area())
Abstraction in Python — output: 12.56. Shape() would fail. Circle fills in area().
How to Explain Abstraction in Python to an Interviewer
Explain Abstraction as if you are teaching a junior — definition, then one tiny script.
Hide complexity. Show useful actions. ABC + abstractmethod.
What does the example print, and why?
Abstraction in Python — output: 12
What mistake do freshers make with Abstraction?
Force child methods.