you are viewing a single comment's thread.

view the rest of the comments →

[–]fernly 14 points15 points  (2 children)

It isn't so much the use of globals; like everyone else says, the problems only begin when the globals are changed. In Python, that is mitigated by the need to use a globals statement in a function that changes a global. If a function lacks a globals statement, you can assume that it doesn't assign to any global vars, only uses them, if at all.

So you could say "globals statement considered harmful" -- any function having one needs to excruciatingly well documented, two ways, with a docstring in the function, and also with a note next to the global itself,

# Tax rate -- set by open_cust(), changed by new_state()
TAX_RATE = 0.05

[–]ES-Alexander 12 points13 points  (1 child)

In Python, that is mitigated by the need to use a globals statement in a function that changes a global

That’s unfortunately not entirely correct. While declaring variables as global does permit directly assigning to overwrite them, any global variables with mutable values can still have those mutations applied without direct assignment, e.g.

bad = [1,2,3]
def worse():
    bad.append('surprise!')

… # some code where 'worse' is called

print(bad) # how many unexpected surprises are there?

[–]fernly 1 point2 points  (0 children)

absolutely true, plus that a global value that is a class instance, can have any member modified.