This question already has an answer here:
- Why is Global State so Evil? 17 answers
I'm a newbie coder. I find it troublesome to declare a variable in 1 function and not be able to access it in other functions. I have to make many of my variables global just to get my code to work. But a lot of people say that the global state is evil. I don't understand the purpose of having limited scope, anyway.
Scope seems to encourage developers to declare same-name variables in different places.
def func1(): string = 'hello world' ... def func2(): string = 'hello universe' ...
Without the need to distinguish variables in different functions, developers name their variables vague and less-meaningful names. Wouldn't it be better to have more specific variable names?
def func1(): world_string = 'hello world' ... def func2(): universe_string = 'hello universe' ...
Why is limiting access to variables outside of the block that it's defined in, i.e. scope, a good thing to have in programming languages? As a newbie, I find it convenient to make every variable a global variable. Why is that bad in bigger projects?