1

Here is an extract of a script I've written:

succ_count = 0

def run_copy():
    shutil.copy(file, 'W://20' + year + '/' + foldername + '/')
    global succ_count
    succ_count += 1

A colleague of mine said that the use of global variables here is bad.

I can see how it's inconvenient because I have to use global succ_count before I increment it by one.

But why is the use of globals considered bad programming?

Apologies if this a very obvious question, I am relatively new to Python and trying to understand things as much as possible.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Joe Smart
  • 751
  • 3
  • 10
  • 28

1 Answers1

1

Global variable are considered bad in almost every programming language. You may check this: Why Global Variables Should Be Avoided When Unnecessary

Also to add the biggest problem to use global variables is that every function has access to these variables, and it is really hard sometimes to figure out which functions actually read and write these variables.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331