0% found this document useful (0 votes)
4 views5 pages

Oops in Python

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts in Python, covering topics such as classes, objects, encapsulation, inheritance, polymorphism, and abstraction. It explains key features like magic methods, advanced OOP concepts, and practical applications, as well as the differences between functions and methods. Additionally, it highlights best practices, pitfalls, and various types of functions in Python.

Uploaded by

arunimadolui270
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Oops in Python

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts in Python, covering topics such as classes, objects, encapsulation, inheritance, polymorphism, and abstraction. It explains key features like magic methods, advanced OOP concepts, and practical applications, as well as the differences between functions and methods. Additionally, it highlights best practices, pitfalls, and various types of functions in Python.

Uploaded by

arunimadolui270
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

OOPS IN PYTHON

---
🔹 Basic OOPs Concepts in Python
1. What is Object-Oriented Programming (OOP)?
2. What are the four pillars of OOP?
3. What is a class in Python?
4. What is an object in Python?
5. How do you create a class and object in Python?
6. What is the __init__ method in Python?
7. What is self in Python classes?
8. What are instance variables and how are they different from class variables?
9. What are class methods and how are they defined?
10. What is the difference between instance method, class method, and static
method?
---
🔹 Encapsulation
11. What is encapsulation in Python?
12. How do you make attributes private in Python?
13. What are getter and setter methods?
14. What is name mangling in Python?
15. Can private variables be accessed outside the class?
---
🔹 Inheritance
16. What is inheritance in Python?
17. How is inheritance implemented in Python?
18. What is single, multiple, multilevel, and hybrid inheritance?
19. How does the super() function work?
20. What happens if two parent classes have the same method?
---
🔹 Polymorphism
21. What is polymorphism in Python?
22. How is method overloading handled in Python?
23. What is method overriding?
24. What is duck typing in Python?
25. Can you give an example of operator overloading?
---
🔹 Abstraction
26. What is abstraction?
27. How is abstraction implemented in Python?
28. What is the abc module in Python?
29. What are abstract base classes?
30. Can a class be both abstract and concrete?
---
🔹 Special/Magic Methods
31. What are magic methods in Python?
32. What is __str__() vs __repr__()?
33. What is the use of __eq__(), __lt__(), etc.?
34. What does __new__() do?
35. How does object creation (__new__) differ from initialization (__init__)?
---
🔹 Advanced OOP Concepts
36. What is composition vs inheritance?
37. What is a metaclass in Python?
38. What is the difference between is and ==?
39. How is memory managed for objects in Python?
40. How does Python implement method resolution order (MRO)?
---
🔹 Practical/Scenario-Based Questions
41. What happens if you call a method on an object that doesn’t exist?
42. What are mixin classes?
43. How would you design a class for a bank account system?
44. When should you use a class vs a dictionary?
45. How would you implement a Singleton pattern in Python?
---
🔹 Best Practices and Pitfalls
46. What are the advantages of OOP?
47. What are some disadvantages or pitfalls of using OOP?
48. Why prefer composition over inheritance?
49. How can Python's dynamic typing affect OOP?
50. What are SOLID principles and how are they applied in Python?
---
Function vs method in Python
✅ Function
A function is a block of code that is defined using the def keyword and not bound
to any object. Can be called [Link] exist outside of a class.
def greet(name):
return f"Hello, {name}"
print(greet("Arunima")) # Output: Hello, Arunima
---
✅ Method
A method is a function that is associated with an object. Defined inside a class and
always takes self (or cls for class methods) as the first [Link] using
object.method_name() syntax.
class Greeter:
def greet(self, name):
return f"Hello, {name}"
g = Greeter()
print([Link]("Arunima")) # Output: Hello, Arunima
---

📊 Key Differences Table


Feature Function Method
Location Can be inside or outside a class Always inside a class
Binding Not bound to object/class Bound to object (self) or class (cls)
Syntax function() [Link]()
Types Regular function. Instance method, class method, static method
First Argument Explicit args only. self, cls depending on type
---
🔁 Example: Both in one place
def standalone_function(x):
return x * x
class MathOps:
def instance_method(self, x):
return x + x
# Call function
print(standalone_function(4)) # 16
# Call method
obj = MathOps()
print(obj.instance_method(4)) # 8

Different types of functions


Instance Method
Class Method
Static Method

Different types of method

Function Type Description


Built-in Provided by Python
User-defined Created using def
Lambda Anonymous, one-liner functions
Recursive Calls itself
Generator Uses yield to return lazy sequences
Higher-order Takes or returns another function
Nested Defined inside another function
Decorators. Modify the behavior of other functions

You might also like