Python - Scope
Code Life
By Pierre Marsaa
Introduction to Variable Scope
In Python, scope refers to the region of code where a variable can be accessed. Understanding scope is crucial for writing bug-free functions.
When you create a variable inside a function, it's called a local variable. This variable only exists inside that function and cannot be accessed from outside.
↗️
Loading editor...
↶
↷
In this example, codemon_name only exists inside the train_codemon() function. If you try to use it outside (uncomment the last line), Python will give you an error saying the variable doesn't exist.
Why Local Variables?
Local variables help keep functions independent and prevent accidental changes to variables. Each function has its own "workspace" with its own variables.
Verify