Onkar Lapate’s Post

In Python, variables are not just a "box" where we store data. In C or C++, a variable is a memory location. When we change the value, we overwrite what is inside that specific "box". In Python, variables are just tags (pointers) stuck onto an object. And everything in Python is object, as I mentioned in last post. How it works? Imagine a balloon floating in a room as an object. A variable is simply a string tied to that balloon. 1. a = [1, 2, 3] -> We create a balloon and tie a string labeled "a" to it. 2. b = a -> We don’t create a new balloon. We just tie a second string labeled "b" to the exact same balloon. 3. a.append(4) -> We changed the balloon. Since "b" is tied to the same balloon, b now also "sees" the change. Why does it matter? This is where Reference Counting gets its name. Python doesn't count how many "boxes" you have. It counts how many strings are currently tied to the balloon. -> Tie a string? Refcount +1 -> Untie a string (del a)? Refcount -1 Being careful while passing variables(objects) inside functions in python is necessary for this reason. I am trying to learn Python Internals in detail and will share my learnings. Do follow along and tell your experiences. #Python #PythonInternals #SoftwareEngineering #BackendDevelopment

  • diagram

To view or add a comment, sign in

Explore content categories