I'm not gonna ask you to demonstrate the `global` keyword but do you think you could elaborate on why I shouldn't be used beyond just "it's evil"? Why is it bad?
Let's start with functions. Functions have their own scope, which means any variables you create inside functions don't affect anything else in the program. This is important as you want functions to be self-contained.
For example, imagine if you created a variable called `text`, let's say. And then imagine you call another function from some module that also happens to have the variable `text` defined within it. If all variables were global, calling this function would have an effect on your `text` variable, probably without you realising what's happening.
This is why functions have their own scope.
In some programming styles, mainly functional programming, this idea is strictly enforced and functions can never have side effects—that is they can never affect anything outside the function.
In Python in general, though, it is possible to make changes to existing variables but only under certain conditions, for example if you pass an object to a function and that function changes that object, then the function is changing something in the main program.
Back to the point. If you define a variable as `global`, you're overriding this important safety feature of functions. It will be much harder to ensure your functions aren't having unwanted side-effects, and it's also harder to follow the code as you need to keep track of what's happening to each variable as you call each function.
If you have an example of code you used where you feel that using `global` is the only solution, post it here and we'll find the alternative that doesn't require `global`!
I mean I know that in functional programming functions are not supposed to have side effects, so for a purely functional programming language, using the global keyword would mess that up, and could even be called evil. But I feel like unless you're claiming that object oriented programming in general is evil, and functional programming is the only virtuous method, you haven't really convincingly explained how the global keyword, in Python, is always evil. Lots of developers write object-oriented code in Python with very little of the functional approach, why is the global keyword evil in this type of code?
And the `global` keyword is never used in OOP in fact. The concepts are different. In OOP, the whole paradigm is about objects changing their state, but that’s happening within an object.
In a well-designed OOP codebase, the changes in state happen only through methods that are part of the class interface.
Note that even methods in a class (which are functions) have local variables. What can change are the data attributes in an object.
Therefore, in OOP, the change of state is part of the class design and carefully controlled (hopefully) by whoever wrote the class. Incidentally, those who dislike OOP (and if you’re a reader of The Stack you know I’m not one of them!) dislike it for this reason.
The global keyword is used to make a variable global across the whole program. There’s (almost) never a reason to do this. It adds a lot of risk, making the code harder to follow, for zero benefit
Thanks - very well described and explained. I seem to recall I got into trouble with methods in classes where I was passing variables in and using the same variable names in the main calling code and the method itself.
Have just written up my own notes on the LEGB article - I have to say that is a masterclass of explanation and the use of the personas (Inna, Otto, Mina) works very well in getting the concepts across
I'm not gonna ask you to demonstrate the `global` keyword but do you think you could elaborate on why I shouldn't be used beyond just "it's evil"? Why is it bad?
Let's start with functions. Functions have their own scope, which means any variables you create inside functions don't affect anything else in the program. This is important as you want functions to be self-contained.
For example, imagine if you created a variable called `text`, let's say. And then imagine you call another function from some module that also happens to have the variable `text` defined within it. If all variables were global, calling this function would have an effect on your `text` variable, probably without you realising what's happening.
This is why functions have their own scope.
In some programming styles, mainly functional programming, this idea is strictly enforced and functions can never have side effects—that is they can never affect anything outside the function.
In Python in general, though, it is possible to make changes to existing variables but only under certain conditions, for example if you pass an object to a function and that function changes that object, then the function is changing something in the main program.
Back to the point. If you define a variable as `global`, you're overriding this important safety feature of functions. It will be much harder to ensure your functions aren't having unwanted side-effects, and it's also harder to follow the code as you need to keep track of what's happening to each variable as you call each function.
If you have an example of code you used where you feel that using `global` is the only solution, post it here and we'll find the alternative that doesn't require `global`!
I mean I know that in functional programming functions are not supposed to have side effects, so for a purely functional programming language, using the global keyword would mess that up, and could even be called evil. But I feel like unless you're claiming that object oriented programming in general is evil, and functional programming is the only virtuous method, you haven't really convincingly explained how the global keyword, in Python, is always evil. Lots of developers write object-oriented code in Python with very little of the functional approach, why is the global keyword evil in this type of code?
And the `global` keyword is never used in OOP in fact. The concepts are different. In OOP, the whole paradigm is about objects changing their state, but that’s happening within an object.
In a well-designed OOP codebase, the changes in state happen only through methods that are part of the class interface.
Note that even methods in a class (which are functions) have local variables. What can change are the data attributes in an object.
Therefore, in OOP, the change of state is part of the class design and carefully controlled (hopefully) by whoever wrote the class. Incidentally, those who dislike OOP (and if you’re a reader of The Stack you know I’m not one of them!) dislike it for this reason.
The global keyword is used to make a variable global across the whole program. There’s (almost) never a reason to do this. It adds a lot of risk, making the code harder to follow, for zero benefit
So pleased to see this article - I have got myself in a right mess with scopes etc in the past - and look forward to working through this
Hope you enjoy it. And find it useful…
Thanks - very well described and explained. I seem to recall I got into trouble with methods in classes where I was passing variables in and using the same variable names in the main calling code and the method itself.
Another topic that’s relevant in that case is how Python uses “pass by assignment” when passing arguments to functions (and methods). Here’s a bit of reading on that topic, too: https://www.thepythoncodingstack.com/p/python-pass-by-value-reference-assignment
Thanks - I will check that out.
Have just written up my own notes on the LEGB article - I have to say that is a masterclass of explanation and the use of the personas (Inna, Otto, Mina) works very well in getting the concepts across
Thank you! Glad you noticed the names weren't just randomly chosen!