Stop. Before you scroll past this, let me ask you something. When you write x = 10 in Python, what do you think happens? If your answer is "Python puts the number 10 inside a box called x," you are wrong. And honestly? That wrong mental model is the reason behind 90% of the confusing bugs beginners run into. I spent a full week going down the rabbit hole of CPython source code, reading PEPs, and running weird experiments in my terminal. What I found completely changed how I write Python. And today, I want to share all of it with you. Let's pull back the curtain on what Python is actually doing with your code. The Biggest Lie You Were Told About Variables Every beginner tutorial starts the same way. "Variables are like boxes. You put values in them." Sounds logical. Feels right. But it is dead wrong in Python. In Python, variables are name tags, not boxes. When you write x = 10, Python does not stuff the number 10 into some container called x. Instead, Python creates an object (the integer 10) somewhere in memory, and then sticks a label called x on it. Think about it like luggage tags at an airport. The tag does not hold your suitcase. It just tells you which suitcase to grab. Let's prove it with actual code x = 10 y = x print(id(x)) # 140712834816848 print(id(y)) # 140712834816848 print(x.__class__) # print(isinstance(x, object)) # True # Even functions are objects! def greet(): return "Hello" print(type(greet)) # print(greet.__class__) # print(isinstance(greet, object)) # True Enter fullscreen mode Exit fullscreen mode Functions, classes, modules, even None itself. All objects. Every single thing in Python lives as an object on the heap. Quick quiz for you What does this print? print(type(type)) Enter fullscreen mode Exit fullscreen mode Answer: . The type of type is type itself. Mind bending, right? This is how Python bootstraps its entire type system. Mutable vs Immutable: The Bug Factory Here is where beginners lose hours of their lives debugging. Immutable objects (int, str, tuple, frozenset) When you "change" an immutable object, Python does not modify the original. It creates a brand new object and re-points your variable to it. x = 10 print(id(x)) # 140712834816848 x = x + 1 print(id(x)) # 140712834816880 0 forever! a = [] b = [] a.append(b) b.append(a) del a del b # Both objects still reference each other. # Refcount never hits 0, but nobody can access them! Enter fullscreen mode Exit fullscreen mode This is where Python's generational garbage collector kicks in. It periodically scans for groups of objects that only reference each other with no outside connections, and cleans them up. import gc # You can see the garbage collector's thresholds print(gc.get_threshold()) # (700, 10, 10) # Generation 0: checked every 700 allocations # Generation 1: checked every 10 Gen-0 collections # Generation 2: checked every 10 Gen-1 collections # Force a collection collected = gc.collect() print(f"Garbage collector freed {collected} objects") Enter fullscreen mode Exit fullscreen mode Python's Secret Speed Hack: Integer Caching Here is something that blows people's minds when they first discover it. # Inside the cache range (-5 to 256) a = 256 b = 256 print(a is b) # True [1, 2, 3] Inside modify(): numbers -----> [1, 2, 3] [1, 2, 3] [10, 20, 30] 4}: a is b = {a is b}") print() print("=" * 50) print("EXPERIMENT 4: String interning") print("=" * 50) s1 = "hello" s2 = "hello" s3 = "hello world" s4 = "hello world" print(f" 'hello': s1 is s2 = {s1 is s2}") print(f" 'hello world': s3 is s4 = {s3 is s4}") Enter fullscreen mode Exit fullscreen mode Run this on your own machine. Watch the outputs. Play with it. Change things. Break things. That is how you really learn this stuff. Cheat Sheet: The Rules of Python's Object Model Concept What Python actually does x = 10 Creates an int object (10), binds the name x to it y = x Binds the name y to the same object x points to x = x + 1 Creates a NEW int object (11), rebinds x to it a.append(4) Modifies the list object in place, all names see the change del x Removes the name x, decrements refcount of its object x == y Compares values (use this!) x is y Compares identity / memory address (rarely use this) copy.copy() New outer container, shared inner objects copy.deepcopy() Completely independent clone at every level Wrapping Up Python looks simple on the surface. x = 10 feels like the most basic thing in the world. But underneath, there is a sophisticated system of objects, references, and memory management working together. Understanding these internals does not make you a "theoretical" programmer. It makes you a better practical programmer because: You will stop writing bugs caused by shared mutable objects You will know when to copy data and when not to You will write more memory-efficient code using __slots__ You will actually understand error messages about mutability You will pass Python interviews with confidence The next time someone says "variables are boxes," you will know better. Now go run those experiments. Break something. That is how this stuff sticks. What concept surprised you the most? Have you ever been bitten by the mutable default argument bug? I would love to hear your stories in the comments.
Python Thinks Different: What Actually Happens Inside Your Code (Visual Guide)
Full Article
Original Source
Read the full article at Dev →KhanList aggregates and links to publicly available news content. We do not host full articles from third-party sources. Always verify important information with original sources.