I'm not an early adopter of python (using it since last versions). I'm very keen to learn the ground concepts and my final aim is to finally dissect and resolve this concetp, so I hope that the following is a mentally demanding question:
I have done my work (due to some personal project I'm developing): I have read this, this, also this and "every" blog post about the global keyword in python (like this).
However, my thoughts are the following:
1) The global keyword must be there for some reasons. ¿Which were the reasons to include it in python core?
2) Regarding its reputation and the possible variable conflicts when using it, from my view, it is a clear example of explicit is better than implicit.
global x -> Hey interpreter, when modify or use the variable x, just make sure that you are using the global variable and not creating a local one (same with nonlocal keyword). ¿What I'm missing to understand its bad prestige?
3) In my project, I need to iterate through a data structure (say a pandas dataframe), apply a function to each row and populate another one (say another np.array) - the populated array must be outside the function because otherwise I would initialize it each time the function is called. For my purpose, using the global keyword makes total sense, so that each time I process a row, I tell the interpreter and hence the function to use that global variable. I don't think of another architechture that makes more sense than this, using ¿the core purpose of the global keyword meaning? ¿Why not?
Obviously, I take care of assigning a name to the variable that doesn't make any conflict with other possible ones.
All the explanations are very welcome. Appreciate it really.
EDIT:
Thanks @DYZ for the references, they elaborate a lot on this concepts. So, my takeaways are:
1) It is crystal clear that global variables "could" be damaging and cause bad code because of its core properties (spaghetti code + side effects). However, their use can be encouraged for simplicity or other related aims when done with consciousness (it is the first time I read something in favour of its ¿core development purpose?).