How does len() function knows how to handle a List, a String, and a Dictionary all the same way in Python? TL;DR: Python Protocols A Protocol is just the set of rules (special methods) an object follows. "Duck Typing" Contract In Python does not care about what the object is, it cares only about what it does. These are represented as dunders (__method__) in Python. 3 Protocols we use everyday in Python: -> Sized (__len__): Tells Python that object has a size. (Powers len()) -> Container (__contains__): Tells Python how to check if something is "inside" the object. (Powers the in keyword) -> Iterable (__iter__): Tells Python that object can be looped over. (Powers for x in obj) Why is this useful? As a developer, we want our code to be intuitive. By following these protocols, we make our custom objects compatible with all of Python’s built-in tools. Protocols are the API of the Python language itself. Using them make custom objects start feeling like native Python parts. I’m deep-diving into the Python protocols this week and will share my learnings. Do follow along and tell your experiences in comments. #Python #PythonInternals #SoftwareEngineering #BackendDevelopment
Python Protocols: Len, Contains, and Iterate
More Relevant Posts
-
Why range(1,000,000) is cheap, but list(range(1,000,000)) is costly in Python? TL;DR: Iteration Protocol in Python needs to know only next item and not full list. The "Next Page" Rule Iteration in Python isn't about having a collection of items; it’s about knowing how to get the next item. Two special methods make this possible: 1. __iter__() → tells Python “I can be looped over” 2. __next__() → returns the next value, one at a time When there’s nothing left, StopIteration tells Python to stop the loop. Why this matters? When we use a list, we pay for all the memory upfront. When we use the Iteration Protocol, we only pay for one item at a time. This is called Lazy Evaluation. Takeaway - If the object represents a collection or a stream of data, implement __iter__ and __next__. It makes the code more memory-efficient and much more "Pythonic." I’m deep-diving into the Python protocols this week and will share my learnings. Do follow along and tell your experiences in comments. #Python #PythonInternals #SoftwareEngineering #BackendDevelopment
To view or add a comment, sign in
-
-
Why .join() is faster than str1 + str2 in Python? Why string concatenation gets really slow in Python? Strings are immutable in Python. When we write str1 + str2, following steps are done - 1. Request a brand new block of memory large enough for both. 2. Copy every single character from str1 and str2 into the new block. 3. Destroy the old strings (eventually) If we do this 10,000 times in a loop, we are copying millions of characters over and over again. When we use ''.join(list_of_strings), Python is significantly smarter. It performs following 2 pass operation - 1. It iterates through the entire list once to calculate the total length of the final string. 2. it allocates one single block of memory of that exact size and copies all the strings into their respective slots at once. Instead of n reallocations and n copies, one allocation and one copy! Takeaway - -> Need to join 2 or 3 strings - a + b is fine and very readable. -> Need to join strings in a loop - Always collect them in a list first and use ''.join(my_list) at the very end. I’m deep-diving into Python internals and performance. Do follow along and tell your experiences in comments. #Python #PythonInternals #SoftwareEngineering #BackendDevelopment
To view or add a comment, sign in
-
-
Day 37 – Understanding How Python Stores Data Today, I am continuing on hash tables in Python, which is what powers dictionaries (dict). In simple terms: Python uses a smart system to store and find data almost instantly, instead of searching line by line. That’s why dictionaries are fast and used everywhere — from logins to APIs to caching. Today, I didn’t just read about how Python dictionaries work — I built a simple hash table from scratch in VS Code. What I did: Created a basic HashTable class Used Python’s hash() function to decide where data should live Stored values in buckets (lists) to safely handle collisions Retrieved values using keys, just like a real Python dict Even tested collisions by inserting keys that land in the same bucket I learned: Why dictionary keys must not change What a hash is (Python’s way of knowing where to store data) Why this concept matters for building fast and scalable systems This might look small, but it’s one of the ideas behind efficient backend and full-stack development. Slow progress is still progress. Understanding beats rushing. Which of the terms or concepts used here sounds too scary and unusual for you? Let me know, let's learn together 😊 #Day37 #LearningInPublic #Python #DataStructures #BackendBasics #Consistency
To view or add a comment, sign in
-
Why Python prefers 𝘵𝘳𝘺 / 𝘦𝘹𝘤𝘦𝘱𝘵 over 𝘪𝘧 𝘦𝘭𝘴𝘦 checks? TL;DR: In Python, exceptions are part of normal flow, not rare events! There are two ways to walk through a uncertain code block - 1. Look Before You Leap (LBYL) - - Check conditions before doing the action - The 𝘪𝘧 𝘦𝘭𝘴𝘦 statements. - But here, Python looks at the dictionary twice. Once to check if the key exists, and once to actually fetch it. 2. Easier to Ask Forgiveness than Permission (EAFP) - Assume things will work and wrap the action in a try block. - If the key exists (which is usually the case), execution stays on the fast path. EAFP is the "Pythonic" approach. It is the way Python is built internally too. Python uses exceptions internally for everything. When using a for loop, Python doesn't check the length of the list; it just keeps asking for items until the list raises a StopIteration exception. Takeaway - -> Using try excepts will not add to any extra processing time, if not reduce it in most of the practical cases. -> Use LBYL if the failure is expected to happen often. -> Use EAFP if the failure is unexpected and rare. I’m deep-diving into Python internals and performance. Do follow along and tell your experiences in comments. #Python #PythonInternals #SoftwareEngineering #BackendDevelopment
To view or add a comment, sign in
-
-
🐍 Ever wondered how Python actually works behind the scenes? We write Python like this: print("Hello World") And it just… works 🤯 But there’s a LOT happening in the background. Let me break it down simply 👇 🧠 Step 1: Python compiles your code Your .py file is NOT run directly. Python first converts it into: ➡️ Bytecode (.pyc) This is a low-level instruction format, not machine code yet. ⚙️ Step 2: Python Virtual Machine (PVM) The bytecode is executed by the PVM. Think of PVM as: 👉 Python’s engine that runs your code line by line This is why Python is called: 🟡 An interpreted language (with a twist) 🧩 Step 3: Memory & objects Everything in Python is an object. • Integers • Strings • Functions • Even classes Variables don’t store values. They store references 🔗 That’s why: a = b = 10 points to the SAME object. ⚠️ Step 4: Global Interpreter Lock (GIL) Only ONE thread executes Python bytecode at a time 😐 ✔ Simple memory management ❌ Limits CPU-bound multithreading That’s why: • Python shines in I/O • Struggles with heavy CPU tasks 💡 Why this matters Understanding this helped me: ✨ Debug performance issues ✨ Choose multiprocessing over threads ✨ Write better, scalable backend code Python feels simple on the surface. But it’s doing serious work underneath. Once you know this, Python stops feeling “magic” and starts feeling **powerful** 🚀 #Python #BackendDevelopment #SoftwareEngineering #HowItWorks #DeveloperLearning #ProgrammingConcepts #TechExplained
To view or add a comment, sign in
-
-
When I review Python code, I often look past syntax and focus on decisions. Take this line: if user_id in users: grant_access() It works. But what matters is what users actually is. A list → Python checks items one by one A set or dict → Python jumps straight to the answer Same line of code. Very different performance. With large data, these choices decide whether a system feels instant or slow. This is the kind of detail that separates: • someone who writes Python • from someone who understands how Python behaves I recently wrote a complete breakdown of how Python searches data internally—linear search, binary search, and hash lookup—using real examples and benchmarks. It’s not about algorithms. It’s about choosing the right data structure upfront. Full breakdown 👇 https://lnkd.in/gT2uaZER #Python #SoftwareEngineering #BackendEngineering #Performance #CodeQuality
To view or add a comment, sign in
-
-
Why creating a tuple is faster than creating a list in Python? TL;DR: Python doesn’t always create new objects, it uses cached ones! Creating an object is expensive, it needs to ask the OS for memory. To avoid this, CPython implementation uses FREE LISTS for immutable objects like Tuples. How does it work? 1. When you stop using a small tuple (up to 20 elements), Python doesn't delete it from RAM. 2. It moves it to a "Free List" (a specialised cache). 3. When you need a new tuple of that same size, Python just grabs an old one from the cache and renews it. Lists, however, are rarely recycled this way because of their dynamic nature makes them too complex to keep in a simple cache. Why this matters? -> In a a real-time game engine, or a data processing pipeline, you might be creating objects millions of times per second. -> The List Tax: Every time you use [a, b], you are potentially triggering a memory allocation request. -> The Tuple Win: Every time you use (a, b), you are likely just grabbing a "pre-warmed" slot from Python’s internal cache. I’m deep-diving into Python internals and performance. Do follow along and tell your experiences in comments. #Python #PythonInternals #SoftwareEngineering #BackendDevelopment
To view or add a comment, sign in
-
-
Tuples in Python: Immutable (cannot be changed after creation), ordered collection that allows duplicate elements. Syntax:() Two methods: Index(),count() Python 3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit (AMD64)] on win32 Enter "help" below or click "Help" above for more information. >>> #tuples() >>> a=(4,5.7,"python",4+9j,True,False) >>> print(a) (4, 5.7, 'python', (4+9j), True, False) >>> type(a) <class 'tuple'> >>> len(a) 6 >>> a.count("python") 1 >>> a.index(True) 4 Pooja Chinthakayala Mam,Saketh Kallepu Sir,Uppugundla Sairam Sir
To view or add a comment, sign in
-
Day 36 – Hash Tables in Python (What’s really behind dict) 🐍 Today, we’re starting with Hash Tables — the idea behind one of Python’s most-used tools: the dict. If you’ve ever written: user = {"name": "John", "age": 25} then you’ve already used a hash table (even if you didn’t realize it). So why start here? Because hash tables help us store and retrieve data fast. Instead of looping through a list item by item, we can jump straight to what we need. That’s why they show up everywhere: user profiles settings and configurations caching quick lookups in real applications Why Python? Python makes this concept very approachable. Dictionaries look simple on the surface, but there’s a lot of smart engineering underneath. Once you understand how they work, you stop writing “just working” code and start writing efficient, intentional code. And yes — this matters for full-stack development too: Backends use hash tables to manage users, sessions, and data Frontends rely on key-value structures for state and UI logic Performance often comes down to how well you organize and access data We’re starting here because this is foundational. When this clicks, many other data structures and algorithms start to make sense. More coming from tomorrow — challenges, breakdowns, and practical thinking. 🚀 #Day36 #Python #DataStructures #HashTables #SoftwareEngineering #FullStackDevelopment #LearningInPublic
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development