Modifying parent variables in python - Don't do this!

Опубликовано: 24 Июль 2026
на канале: InvalidEntry
134
5

A request from a viewer on how to modify variables in the callers scope - its doable, but it has a lot of bad reasons why you should. Python has a super open scope, which allows you to interact with both the variables and code of other objects. Its flexibility makes it super good for writing easy to read business logic, but can have issues if trying to write high security applications.

Python is not the only language which suffers - most other high level languages also contain features which allow the language to be turned against itself; they all contain the concept of stacks, frames, and usually through the exception system, allows a programmer to produce a reference to higher level languages.

This is not recommended though - it forms a tight coupling between the caller and callee, and will generally lead to un-usable sub functions (i.e. breaks the DRY pattern).