How does Generational Garbage Collector (GC) clean up circular references in Python? Python doesn’t scan all objects for circular references all the time. That will be time-taking. It divides objects into 3 different generations - Generation 0 → Every new object starts here, cleaned very frequently Generation 1 → Objects that survived Gen 0, cleaned less often Generation 2 → Long-living objects (configs, globals, caches), cleaned very rarely Objects that survive cleanup get promoted to the next generation. The Generational GC asks a question: "If I ignore references from the outside world, are you only pointing at each other?” If yes → Python breaks the cycle → reference counts drop → memory is freed. There can be tiny “random” latency spikes in Python apps, often the GC waking up especially for Generation 2. Reference Counting and Generational GC together make memory management feasible in Python. I am trying to learn Python Internals in detail and will share my learnings. Do follow along and tell your experiences in comments. #Python #PythonInternals #SoftwareEngineering #BackendDevelopment
Onkar Lapate’s Post
More Relevant Posts
-
Mastering Python: A Quick Tip on lambda and filter() 🐍💡 Ever wondered how to make your code cleaner and more Pythonic for simple list operations? The lambda and filter() functions are your best friends! They are especially useful for concise, one-off functions where defining a full def block might be overkill. Here’s a simple example of how to filter even numbers from a list: python nums = [1, 2, 4, 5, 6, 7, 8, 9, 10] # Using a lambda function with filter() to get even numbers evens = filter(lambda x: x % 2 == 0, nums) print(list(evens)) # Output: [2, 4, 6, 8, 10] Use code with caution. ✅ Why this is great: Concise: Achieves a specific task in a single, readable line. Efficient: filter() returns an iterator, which is memory efficient for large datasets. Functional: Showcases a core concept of functional programming in Python. #Python #LambdaFunctions #CleanCode #PythonProgramming #TechTips #DataScience# Abhishek kumar # Harsh Chalisgaonkar # SkillCircle™
To view or add a comment, sign in
-
-
While working with Python, I noticed something curious. When you assign a value to a variable, then change it, the object’s memory address changes. That’s expected. But if you later assign the same value again,Python gives you the exact same address as before. At first glance, this feels like Python is somehow “remembering” the old location. But that’s not what’s happening. What’s really going on? In CPython (the most common Python implementation), there is a mechanism called interning / caching. CPython pre-allocates and reuses certain immutable objects, most notably: Small integers in the range -5 to 256 Some short strings and identifiers So when you write: a = 10 b = 10 Both a and b usually point to the same object in memory. That’s why id(a) == id(b) is often True. Now compare that with larger integers: a = 10000 b = 10000 Here, you’ll often get different memory addresses. These values are not guaranteed to be cached, so Python may allocate new objects. Why does Python do this? This design has very practical benefits: Saves memory by reusing common immutable objects Reduces object allocations Lowers pressure on the garbage collector Improves performance for frequently used values Since integers and strings are immutable, sharing them is completely safe. #python #coding #LearningJourney #DeveloperJourney #Insights
To view or add a comment, sign in
-
-
How Python Manages Memory: Mutable vs Immutable One concept that silently affects performance, bugs, and behavior in Python is mutability. Immutable objects 👉 int, float, str, tuple Their value cannot be changed in place Any “modification” creates a new object in memory Safer, predictable, hashable (used as dict keys) Mutable objects 👉 list, dict, set Can be modified without changing memory reference Faster updates, but risk of unexpected side effects Changes reflect across all references Why this matters in real projects - Unexpected bugs when modifying lists passed to functions - Memory inefficiency when repeatedly modifying strings - Confusing behavior in function arguments & shared data #Python #MemoryManagement #Mutable #Immutable #PythonInternals #SoftwareEngineering
To view or add a comment, sign in
-
Although it may not be a big deal in other object oriented language to know the difference between Class and Object, it is a big deal in Python. This is especially evident and important when using libraries and/or frameworks that expects you to pass a callback function as one of the arguments to its own function or method. Why Python? Cause Python expects an extra "self" parameter to be passed in instance methods that other PLs don't. You'll encounter this unique behavior when you pass a class, instead of an object, along with the callable as this is a function (not a method) that expects "self", thus you get too many arguments, expected "n" arguments but got "n+1"... type of error, because of that extra argument being passed by Python. And this is why I always tell people that I mentor to not ignore or skip the basics or essentials, as this knowledge becomes critically valuable in most edge cases. #python #class #object
To view or add a comment, sign in
-
Hello Python folks Quick brain-teaser for you: What does this print — and why? t = ([1, 2],) t[0] += [3] print(t) Did the tuple mutate? Or did Python just trick your mental model? Most people answer quickly. Very few explain it correctly. If you can explain this in terms of identity, mutation vs rebinding, you truly understand Python. Full breakdown here including👇 - Shallow Vs Deep Copy - Default Mutable Argument Trap https://lnkd.in/e_v6nNej #python #pythoninterview #softwaredevelopment
To view or add a comment, sign in
-
Do you know what Python's "Enclosing scope" really is and why it exists? First, thank you for the amazing response on my previous article about 👉 Python Mutability vs Immutability https://lnkd.in/gfdypCXD Your feedback really motivated me to go deeper into Python internals Today, I've published Day 3 of the series: 👉 Python LEGB Rule Explained: Scope, Closures, nonlocal, & Late Binding https://lnkd.in/gkRuSemf Most developers understand local and global scope… but the real confusion starts with the enclosing scope. That “middle layer” is the reason behind: • Closures remembering values • nonlocal existing • Late binding bugs in loops • UnboundLocalError confusion Once this clicks, your mental model of Python changes completely. Let me ask you this 👇 When was the first time Enclosing scope confused you in Python? #python #programming #devcommunity #softwareenginerring
To view or add a comment, sign in
-
𝗣𝘆𝘁𝗵𝗼𝗻 𝗗𝗮𝗶𝗹𝘆 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 | 𝗛𝗮𝗰𝗸𝗲𝗿𝗥𝗮𝗻𝗸 – 𝗦𝘁𝗿𝗶𝗻𝗴 𝗠𝘂𝘁𝗮𝘁𝗶𝗼𝗻𝘀 | 𝗗𝗮𝘆 𝟭𝟲 This Python error exposes who really understands strings. Day 16 of my Python Daily Challenge 🚀 Today’s problem looked confusing at first: 👉 Access a string index 👉 Try to change it 👉 Python throws an error 😅 That’s when the real lesson hit 👇 • Strings in Python are 𝗶𝗺𝗺𝘂𝘁𝗮𝗯𝗹𝗲 • You don’t modify — you rebuild • Slicing + concatenation = clean solution 💡 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗽𝗮𝘁𝘁𝗲𝗿𝗻 𝗳𝗿𝗼𝗺 𝗗𝗮𝘆 𝟭𝟲: Many Python bugs come from forgetting which data types can and can’t change. Understand 𝗺𝘂𝘁𝗮𝗯𝗶𝗹𝗶𝘁𝘆, and half your errors disappear. Have you ever tried to modify a string directly? Be honest 👇 #Python #HackerRank #DailyCoding #ProblemSolving #InterviewPrep #LearnInPublic #Consistency
To view or add a comment, sign in
-
-
🚀 Post #351 — Learning Python the Right Way Most people can write this in Python: a = 10 But when I asked where does a actually live in memory? Silence. That’s the gap between using Python and understanding Python. 🧠 In Python, variables don’t store values. They store references to objects. a = 10 print(id(a)) 🔍 id() gives you the memory address (identity) of the object a points to. Why this matters in real systems 👇 • Explains immutability (int, str, tuple) • Prevents bugs in shared references & mutability • Helps debug weird behavior in lists, dicts, function calls • Builds a strong base for performance + memory reasoning Example that changes how you think: a = 10 b = 10 print(id(a) == id(b)) # True (integer caching) Python is doing memory optimization, not magic. If you skip internals like this, you’ll write code — but you won’t reason about it. Curiosity at the memory level is what separates script writers from engineers. 🐍 #Python #SoftwareEngineering #BackendDevelopment #LearningInPublic #ComputerScience
To view or add a comment, sign in
-
-
📘 Python Fundamentals: Expressions & Operators Expressions and operators are the backbone of every Python program. This visual breaks down how Python evaluates values and performs actions to produce results. 🔹 What is an Expression? An expression is a combination of values, variables, and operators that Python evaluates into a single result — just like a mathematical sentence. 🔹 Anatomy of an Expression Operands are the values, operators define the action, and together they produce a result (e.g., 10 + 5 = 15). 🔹 Types of Operators Covered ✔️ Arithmetic Operators – perform mathematical calculations ✔️ Comparison Operators – compare values and return True/False ✔️ Logical Operators – combine multiple conditions ✔️ Assignment Operators – assign and update variable values 🔹 Real Python Examples The poster also includes practical code examples to show how expressions work in real programs. Perfect for beginners building strong Python fundamentals and for anyone revising core concepts. #Python #PythonProgramming #LearnPython #PythonForBeginners #CodingTips #Programming #TechEducation
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