A class defines the structure and behavior of objects. It can contain attributes, methods, class attributes, properties, and special methods.
Example
python
class Student:
def study(self):
print("Studying")
s = Student()
s.study()
Important Point
Python classes are objects too and support dynamic behavior.
Question 2: What is an object?
Ans
An object is an instance of a class with its own state and behavior. Multiple instances of the same class can hold different attribute values.
Example
python
class Student:
pass
a = Student()
b = Student()
a.name = "Amit"
b.name = "Neha"
Important Point
Object identity is distinct from object equality.
Question 3: What is `__init__`?
Ans
__init__ is an initializer method called after a new instance has been created. It commonly assigns initial instance attributes.
Example
python
class User:
def __init__(self, name):
self.name = name
u = User("Amit")
print(u.name)
Important Point
`__init__` initializes an object; `__new__` is involved in creating the instance itself.
Question 4: What is self?
Ans
self is the conventional name for the instance reference passed to an instance method. It lets the method access that object's attributes and other methods.
Example
python
class User:
def __init__(self, name):
self.name = name
def greet(self):
return f"Hello {self.name}"
Important Point
`self` is not a reserved keyword, but using the conventional name is strongly recommended.
Question 5: What is class variable?
Ans
A class variable is an attribute stored on the class and normally shared by instances unless an instance shadows it with its own attribute.
Example
python
class Employee:
company = "ABC"
print(Employee.company)
Important Point
Be careful with mutable class attributes because changes can be visible through every instance that shares the same object.
Question 6: What is instance variable?
Ans
An instance variable belongs to a particular object and is commonly assigned through self in __init__ or another method.
Example
python
class User:
def __init__(self, name):
self.name = name
a = User("A")
b = User("B")
Important Point
Each instance has its own binding for normal instance attributes.
Question 7: What is inheritance?
Ans
Inheritance lets a class reuse and extend behavior from a parent class. Python supports single and multiple inheritance.
Example
python
class Animal:
def speak(self): print("sound")
class Dog(Animal):
pass
Dog().speak()
Important Point
Use inheritance when the subtype relationship is meaningful; composition is often better for reusable components.
Question 8: What is multiple inheritance?
Ans
Multiple inheritance allows a class to inherit from more than one parent class.
Example
python
class A:
def a(self): print("A")
class B:
def b(self): print("B")
class C(A, B):
pass
C().a(); C().b()
Important Point
Python resolves methods using MRO, so understand the hierarchy before relying on multiple inheritance.
Question 9: What is MRO?
Ans
Method Resolution Order is the order Python follows when searching classes for attributes and methods. Python uses C3 linearization for modern class hierarchies.
Example
python
class A: pass
class B(A): pass
print(B.mro())
Important Point
MRO becomes especially important with multiple inheritance.
Question 10: What is polymorphism?
Ans
Polymorphism means code can operate on different objects through a common behavior without requiring the exact concrete type. Python commonly achieves this through duck typing.
Example
python
class Dog:
def speak(self): return "bark"
class Cat:
def speak(self): return "meow"
def talk(animal): print(animal.speak())
Important Point
Python often cares about supported behavior rather than a shared explicit parent class.
Question 11: What is encapsulation in Python?
Ans
Python encourages encapsulation through conventions, properties, and controlled APIs rather than strict private enforcement. A leading underscore indicates internal use by convention.
`__name` triggers name mangling; it does not create absolute security or true private storage.
Question 12: What is abstraction?
Ans
Abstraction exposes the operations callers need while hiding implementation details. Python can use abstract base classes from the abc module.
Example
python
from abc import ABC, abstractmethod
class Payment(ABC):
@abstractmethod
def pay(self, amount): pass
Important Point
An abstract base class can enforce that concrete subclasses implement required abstract methods.
Question 13: What is @property?
Ans
property lets a method be accessed using attribute syntax and is commonly used to control reading, validation, or computed values.
Example
python
class Circle:
def __init__(self, r): self.r = r
@property
def area(self): return 3.14 * self.r * self.r
print(Circle(2).area)
Important Point
Properties are useful for preserving a clean public API while changing implementation details internally.
Question 14: staticmethod vs classmethod?
Ans
A static method receives no automatic instance or class argument. A class method receives the class as cls and is useful for alternate constructors or class-level behavior.
Example
python
class User:
@staticmethod
def normalize(name): return name.strip().lower()
@classmethod
def guest(cls): return cls("Guest")
def __init__(self, name): self.name = name
Important Point
Choose classmethod when behavior needs the class itself; staticmethod when no automatic context is required.