



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Module 5: Object-Oriented Programming Organize larger applications with structured, reusable code. OOP lets you define your own objects with custom attributes and behaviors - built around four core principles: Encapsulation, Abstraction, Polymorphism, and Inheritance. Everything Is a Class Python follows the " first-class everything " principle functions. OOP becomes essential as applications grow, every value is an instance of a class, including integers, lists, and letting you define custom blueprints for structured, repeatable code.
Polymorphism: One Name, Many Forms Different classes can share the same method name. Python executes the correct behavior based on the object passed enabling universal functions that work across multiple types.
Special Methods: Python's Magic Dunder methods (double underscore) are triggered automatically by Python syntax - you rarely call them directly. They give your objects built-in behavior.
1. init() Initializes a new object on creation 2. str() Controls how print() displays the object
3. len() Enables 1en() on your custom objects 4. del() Runs when an object is deleted from memory OOP in Action: The Full Picture These four principles work together to make Python code organized, scalable, and maintainable. - Encapsulation Bundle data and methods inside a class, controlling access and protecting state. - Abstraction Hide complex internals; expose only what users need to interact with.
**from module_name import *** Imports everything from the module. (Use cautiously, as it consumes more memory).
from module name import function name Imports only specific attributes, saving memory and removing the need for a prefix.
Important Handling Techniques Catching Specific Exceptions It is best practice to specify the error you are looking for (e.g., except ValueError : or except ZeroDivisionError :). This allows you to handle different types of errors in distinct ways rather than using a blanket catch-all. Raising Exceptions You can manually trigger an exception in your code using the raise keyword (e.g., raise ValueError("Invalid number")). This is useful for enforcing your own constraints on user input. Catching Multiple Exceptions You can handle multiple specific errors in one line using a tuple : except (TypeError, ZeroDivisionError):. Using finally for Cleanup The finally block is essential when dealing with external resources like network connections, databases, or file operations. It guarantees that the resource is safely closed or disconnected, even if the program encountered a fatal error beforehand. MODULE 7: Decorators & Generators Two Features, One Goal: Better Python Both decorators and generators solve real problems - but in fundamentally different ways. Python Decorators Purpose : Extend or modify a function's behavior Syntax : The [@] symbol, functions as objects Benefit : Cleaner, modular, and "Pythonic" code Python Generators Purpose : Produce a sequence of values over time Syntax : The yield keyword, state suspension Benefit : Massive memory savings for large datasets LESSON 1: Python Decorators Decorators are functions that wrap around another function to change its behavior - without modifying the original code. To understand them, you first need to understand how Python treats functions. Functions Are First-Class Objects In Python, functions are just like integers, strings, or lists they can be assigned, passed around, and manipulated freely. Assign to Variables A function can be stored in a variable and called through it later. Pass as Arguments Functions can be passed into other functions to be executed inside. Return from Functions A function can return another function object omit () to pass the object itself, not execute it.
Nested Functions & Scope Functions Within Functions You can define an inner function inside another function. Due to Python's scope rules, the inner function is invisible to the rest of the program -it exists only within its parent. Variable Scope Each function creates its own local namespace. Variables defined inside are separate from global variables, preventing naming collisions and unintended side effects. Returning an inner function without parentheses () passes the object-not the result of calling it. The @ Decorator Syntax Manually wrapping functions is tedious. Python automates this with the @symbol placed directly above a function definition. </> Define wrapper @ Apply @decorator
LESSON 2: Iterators & Generators Generators produce sequence data on the fly rather than calculating an entire list upfront. This is critical for performance when working with massive datasets or infinite sequences. Yield & State Suspension The yield Statement Generators replace return with yield. When hit, the function returns a value and completely suspends its state - remembering exactly where it left off for the next call. One Value at a Time Because the generator pauses while awaiting the next instruction, it only ever holds one value in memory at a time - no matter how large the sequence. Regular Function