0

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?).

Ezarate11
  • 439
  • 6
  • 11
  • @Jayjayyy thanks for the time. Regarding your question: I actually pass the array to the function; however, I need to modify the outside array, not only access it. If I pass it as a parameter and modify it each time I call the function, I don't get a "global" modified array (maybe doing something wrong). However, my question goes more "philosophical": why not use global for that? – Ezarate11 Dec 10 '18 at 23:46
  • 1
    Yeah, you're doing something wrong. Maybe you're assigning to a local variable instead of modifying the actual array. See https://nedbatchelder.com/text/names.html. – user2357112 Dec 11 '18 at 00:03
  • Thanks again @Jayjayyy. That is a good reference I didn't found before. I somehow now understand that the idea behind global is "minimizing risks", due to its core properties, althought it could be used depending on the aim and with full conciousness. – Ezarate11 Dec 11 '18 at 00:06
  • Very thankful for the link. I know see the error: I was assigning the same name to the parameter and to the outside array. My fault. Now I see clearly this alternative. Thanks again and thanks also to @Jayjayyy. – Ezarate11 Dec 11 '18 at 00:18
  • 1
    `global` is most useful when preserving some sort of scalar (immutable) state variable. It can be used and modified in a bunch of functions, without having to be passed as argument and returns. But it's not as useful when working with mutable objects, lists and dictionaries in plain Python, dataframes and arrays in pandas and numpy. It also isn't common in well designed OOP code - instance and class attributes store state. – hpaulj Dec 11 '18 at 00:53
  • Thanks @hpauj. This makes me review and deepen into OOP for the kind of development I have actually. – Ezarate11 Dec 11 '18 at 01:25

1 Answers1

0

The global keyword is used inside a function to declare the variable as global and thereby accessible anywhere in the script. HOWEVER, it should be avoided if at all possible.

foo():
     global x
     x = 5
foo()
print(x)

The console will print 5. Had you not declared x as global, it would not. They should only be used for constants:

pi = 3.14
foo():
     pi = 1
     global pi
     print(pi)
foo()

This will print 3.14.

  • This really doesn't answer any of the questions, together with not even having read the intiial question (the links are there because I actually know what the keyword does). – Ezarate11 Dec 10 '18 at 23:41
  • It is not really necessary to know the purpose or implementation of the `global` keyword because there are basically NO times when you would want to use it. – question asker Dec 10 '18 at 23:55
  • In that case, could you provide the reasons why the core developers put the global keyword there? I don't think that their was to just confuse everybody. – Ezarate11 Dec 11 '18 at 00:03
  • 1
    Python started off as scripting language, competing with languages like Perl and shells. And while everything in the language is an object, you don't have write your code in OOP style (as in Java). Nor are you restricted to functional patterns. I suspect defined globals are most useful in scripting uses, where programming style is casual and scope limited. – hpaulj Dec 11 '18 at 04:03
  • Note that the Python FAQ, https://docs.python.org/3.7/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python, says it seeks to balance usefulness and clutter. Implicit globals don't require declaration; the key word's only needed when you intend to modify a variable inside a function. – hpaulj Dec 11 '18 at 04:08
  • Thats an interesting view. The scripting nature seems logical to me being one of the causes; little scripts with not much risk of side effects and simplicity of use. Thanks for the explanation @hpaulj. – Ezarate11 Dec 11 '18 at 10:00
  • The global keyword is intended for global constants such as pi. There is just no distinction between variables and constants in python. – question asker Dec 11 '18 at 13:37
  • @question, a constant, defined at the top of the module, and never assigned inside a function, doesn't need the `global` keyword. – hpaulj Dec 11 '18 at 13:53
  • It does if you create a new local variable with the same name and wish to access the global constant. – question asker Dec 11 '18 at 14:41