Python vs JavaScript: Variable Scope Boundaries

Python teaches boundaries in the most unexpected ways 😅 Functions really have personalities: Local variables: “I’m only here for this function.” They live inside a function and disappear once it’s done. Global variables “I belong to everyone.” Accessible anywhere in the file… but if you modify them carelessly, your whole program feels it. Nonlocal variables “I’m not global, but I’m not just local either.” They live in an outer function and can only be modified in an inner function if you explicitly declare nonlocal. Here’s what stood out to me: Coming from JavaScript, I was used to closures just working. In JavaScript, if an inner function sees a variable in its outer scope, it can modify it directly. No special keyword. In Python? If you want to modify a variable from an enclosing function, you must explicitly say nonlocal. If you don’t, Python quietly creates a new local variable instead. Same concept. Different philosophy. JavaScript assumes intent. Python requires you to declare it. And that small difference changes how you think about state, structure, and responsibility in your code. Scope isn’t just syntax. It’s architecture. Boundaries matter. In code and in life. #Python #JavaScript #BackendJourney #SoftwareEngineering #WomenInTech

  • diagram

To view or add a comment, sign in

Explore content categories