Magic methods in python : starts and end with '__' . They are called automatically. We can't create our own magic methods, they are predefined, but we can modify the existing magic method for our custom logic in our class.
Example :
__init__ -> called when obj of a class is created.
__str__ -> called when print() is executed
__add__ -> called when + is found
We can modify it in our class:
Look carefully,
What is self here?
It will always point to latest/current object.
Now here,
What if you don't pass self?
When the function is passed, the corresponding obj is always passed as an argument. So, it is mandatory to write self in function parameters.
Let's say, below code is written inside the class.
Observe that, instead of calling method directly, we are calling it via object (self).
why? because, method inside class cannot call each other directly, methods are accessible via object only.
Encapsulation
What if someone changes the value of your instance variables, such as pin or balance.
so, you need to keep them private, In jave/c/c++ private self.balance
but, in python, you use '__' , so, self.__balance and you have to write it like their during entire class, __ is important.
same for method as well:
def __menu():
return
Notice that in the picture, after sbi. there is no visibility for private variables and methods.
Concern :
So, why this?
First, lets understand that, here when some variables are crucial, then, we make it private so that its value doesn't get change by someone else during code. but, there can be a case that, on purposely, for some functionality, you need to use this variable, so that's why python provides this functionality, so, its like a mutual agreement between developers that ok '__xxx' is a private method/dev, dont use it randomly anywhere.
Getter Setter Methods:
Now, there is this confusion, that i was able to change variable's value directly, so whats the purpose of making it private and then accessing it via setter method?
The answer is, you can control the logic inside function, lets say verified user, pin must be 4 digits etc.
So, this entire thing is called Encapsulation :
class diagram:
-class name
-variables
-methods
- = private
+ = public
sbi=Atm()
hdfc=Atm()
here sbi, hdfc are called as 'reference variables'
By default, its pass by reference for class objects.
so, class objects are also mutable just like list.
So, this is risky, to avoid this :
The key difference is about mutability and references:
change(l1):
Passes the original list object to the function
Any modifications inside change() will affect the original list l1
Both l1 and the parameter in change() point to the same memory location
change(l1[:]):
Passes a shallow copy of the list to the function
The original list l1 remains unchanged regardless of what happens inside change()
Creates a new list object with the same elements
Note:l1[:] creates a shallow copy, so if your list contains mutable objects (like nested lists), those inner objects can still be modified. For complete protection with nested structures, you'd need copy.deepcopy().
Here's an example showing how shallow copy with l1[:] doesn't protect nested mutable objects:
defchange(lst):
lst[0][0]=999# Modify nested list
lst.append([7,8])# Add new element# Original list with nested lists
l1 =[[1,2],[3,4]]print("Original l1:", l1)# [[1, 2], [3, 4]]# Case 1: Pass shallow copy
change(l1[:])print("After change(l1[:]):", l1)# [[999, 2], [3, 4]] - nested list modified!# Reset
l1 =[[1,2],[3,4]]# Case 2: Pass original list
change(l1)print("After change(l1):", l1)# [[999, 2], [3, 4], [7, 8]] - both changes applied
Output:
Original l1: [[1, 2], [3, 4]]
After change(l1[:]): [[999, 2], [3, 4]]
After change(l1): [[999, 2], [3, 4], [7, 8]]
What happened:
l1[:] creates a new list, but the nested lists [1, 2] and [3, 4] are still the same objects
When we modify lst[0][0] = 999, we're changing the contents of the nested list, which affects the original
The append() operation only affects the copy, not the original
see, here also, it is pass by reference only, but when you make changes, new copy is created.
Collection:
collection is basically a concept where we store objects in list , tuple etc.
Instance Variable:
A variable that belongs to a specific object and has different values for each object. Each object has its own copy.
Example: pin, balance (each account has different pin/balance)
Static/Class Variable:
A variable that belongs to the class itself and is shared by all objects of that class. All objects see the same value.
Example: bank_name, ifsc_code (same for all accounts in that bank)
Key difference: Instance variables are unique per object, while static/class variables are shared across all objects of the same class.
Notice that, the variable 'counter' is placed outside __init__ , but it is inside the class.
Also, it is not self.counter but Atm.counter, which means class name. variable name.
1. No need to pass self in method.
2. Decorator '@staticmethod' --> add when you are dealing with static variables.
Basic Concept:
A decorator is a function that takes another function as input and returns a modified version of it.
savings acc and curr acc 'is-a' account : inheritance
bank 'has-a' branch : composition
Inheritance
Inheritance :
1. you can inhertit data members, methods and constructor
2. you cannot inherit variables/methods with '__'(private)
method inheritance :
constructor inheritance :
private attribute can't be inherited :
Polymorphism :
Method overriding :
same method in parent and child class.
Method of child class will be executed.
If you want that method of parent should be executed, then, use 'super()'.
- super keyword works inside class only.(that too with constructor and methods only, not with attributes)
- you cannot access it outside the class - s.super().buy() is wrong, no such thing exists.
case where constructor in both class :
output :
Example:
o/p : 100, 200
Types of inheritance in python :
Multiple inheritance :
buy method is in both class, but it will be executed from the 'Phone' class because it is first in the parameter. - this is MRO rule (method resolution order)
Ex1
Ex2
Polymorphism
This code is valid in c and java.
But, its not valid in python, the area() method will get rewrite.
There is no concrete concept for method overloading in python, but we can make it work by writing some logic.
Operator overloading :
3+4 = 7
'+' works as addition and concatenate
Abstraction
abstract means - hidden
abstract method - there is no code inside it
concrete method - our normal method
1. abc = abstract base class
2. What is abstract method?
it is a method where we want that, if someone is inheriting from my class, he must implement this method in his class.
3. decorator @abstractmethod is compulsory
4. In the class where you use this decorator, it must be inherited from ABC
If you don't implement the method, you will get an error.
what is the need for this?
to maintain code structure.
What is abstract class?
-a class which contains at least 1 abstract method.
YOU CANT CREATE OBJ OF ABSTRACT CLASS.
----------------------------------- END --------------------------------------------
In this blog, we'll explore a complete Python solution that detects and extracts tables and text from images using libraries like Transformers, OpenCV, PaddleOCR, and easyOCR. This step-by-step breakdown includes code to detect tables, extract content from individual table cells, and retrieve any remaining text in the image. Overview When working with scanned documents, such as invoices or forms, it is essential to accurately extract both structured information (like tables) and unstructured text. The approach we’ll explore uses Microsoft's pretrained object detection model to locate tables and OCR techniques to extract the text from both table cells and the rest of the image. Steps: 1. This code first detects table using microsoft's model. and save that image which contains detected table only 2. After that, from the detected table , we make a seperate image for each cell. 3. Then we read text from the image of each cell 4. Now, to read the extra texts except for the ...
1. Why Position Matters in Transformers? Transformers rely on self‑attention, which processes tokens in parallel. This means, unlike RNNs, they don’t inherently know the order of words. So, sentences like “Ravi killed the lion” vs. “The lion killed Ravi” would look identical to a vanilla Transformer—clearly problematic! 🧪 Idea #1: The Naïve Approach A simple fix would be to add index/position of the token in an embedding vector. Issues: Unbounded values: Position IDs can become huge (e.g. 100,000+ in long texts), destabilizing training. Discrete steps: Sharp jumps between integers disrupt gradient flow. 🧪 Idea #2: Normalize the Position Numbers What if we divide the position numbers by a constant to make them small and smooth? That helps a bit—values don’t explode anymore. Issues: Now, if you observe, in both the sentences, the word at second position has got the different values. 1 for sentence1, and 0.5 for sentence2. so, the Neural network will get confused while training, what a...
LangChain is a powerful framework for building applications with Large Language Models (LLMs). One of its core features is the Chain Component , which allows developers to link multiple operations together to create complex workflows. In this blog, we'll explore different types of chains in LangChain and demonstrate their usage with code examples. 1️⃣ LLMChain (Basic Single-Step Chain) 🔹 Executes a single LLM call based on a prompt. 🔹 The simplest chain type. ✅ Use Case: When you need a single prompt-response from an LLM. Example: 2️⃣ SequentialChain (Step-by-Step Execution) 🔹 Executes multiple chains in sequence where each step depends on the output of the previous step. ✅ Use Case: Multi-step processes where each step depends on the previous output . 📌 Example: Blog Outline → Content Expansion → Summary Example: 3️⃣ SimpleSequentialChain (Basic Sequential Execution) 🔹 Similar to SequentialChain, but only passes the last step’s output forward (less flexible). 🔹 ...
Comments
Post a Comment