Posts

Showing posts from July, 2025

Why You Should Never Modify a Dictionary While Looping in Python 🐍

Image
Have you ever tried to modify a dictionary while looping over it in Python—only to get hit with a confusing RuntimeError ? You're not alone. This is a classic Python trap that even experienced developers can fall into. ❗ The Problem my_dict = {'a': 1, 'b': 2, 'c': 3} You might try this: for key, value in my_dict.items(): if value == 2: del my_dict[key] But Python will throw: RuntimeError: dictionary changed size during iteration πŸ” Why This Happens Python loops through the internal structure of the dictionary. If you change that structure mid-loop by adding or removing keys, Python raises an error to protect your program from unpredictable behavior. ✅ The Right Way: Loop Over a Copy for key in list(my_dict.keys()): if my_dict[key] == 2: del my_dict[key] By using list(my_dict.keys()) , you're making a safe copy of the keys before the loop begins. Now you can modify the dictionary freely inside the loop. ⚠️...

πŸ” Rotate Multiple Variables in Python Using Just One Line — Here’s How

Image
If you’ve ever tried swapping variables in Python, you probably used something like this: temp = a a = b b = c c = temp Or maybe a slightly cleaner two-variable swap: a, b = b, a But what if I told you that you can rotate three or more variables in just a single line ? No loops. No temporary variables. Just pure Pythonic elegance. ✅ The Trick: Tuple Unpacking a, b, c = 1, 2, 3 a, b, c = c, a, b After executing that, a = 3 , b = 1 , and c = 2 . You've rotated the values clockwise! πŸŽ‰ 🧠 How Does It Work? In Python, the expression on the right-hand side is evaluated as a tuple: (c, a, b) Then Python unpacks it into the variables on the left-hand side : a, b, c This all happens in a single step , so there’s no need for temporary storage like in most other languages. πŸͺ„ Why Is This Useful? ✅ Cleaner syntax ✅ No need for temp variables ✅ Reduces chances of bugs ✅ Makes your code look modern and Pythonic ⚠️ Bonus Tip You can extend this log...
Image
Can You Override Static Methods in Java? Not Quite. Here’s Why. πŸ“Ί Watch This Explanation in 15 Seconds! If you're learning Java or preparing for interviews, you've probably heard that everything in Java is an object . But what happens when you try to override a static method ? The result can be surprisingly misleading. In this post, I’ll walk you through a common misconception about static methods in Java — that they can be overridden. Spoiler alert: they can’t . But don’t worry — we’ll break it down simply, with code examples that clarify exactly what’s going on. πŸ“Œ Static Methods: A Quick Refresher In Java, a static method belongs to the class , not the instance of the class. This means you don’t need to create an object to call it: class Utility { public static void sayHello() { System.out.println("Hello, world!"); } } // Usage Utility.sayHello(); 🚨 The Static Override Illusion Let’s say you have a parent class and a child clas...

Why String a = null; a += 1; Doesn’t Throw NullPointerException in Java

Image
Why String a = null; a += 1; Doesn’t Throw NullPointerException in Java Java is usually strict about using null . Try calling a method on null , and you’ll immediately see a NullPointerException . But here's an interesting case that behaves differently: String a = null; a += 1; System.out.println(a); // Output: "null1" Yes, the code runs fine and prints null1 — no crash, no error. Let’s understand what’s going on behind the scenes. The Trick Behind the Code The line a += 1; is syntactic sugar for: a = new StringBuilder(String.valueOf(a)).append("1").toString(); Now here's what happens step by step: String.valueOf(null) returns the string "null" , not an error. append("1") adds 1 to it, resulting in "null1" . So, your null is automatically converted into the string "null" . That's why there's no NullPointerException . Why Does Java Work This Way? In Java, the + operator for Stri...