title: python: proper usage of global variable
body: <p>here's the code!</p>
  
  <pre><code>import csv
  
  def do_work():
        global data
        global b
        get_file()
        samples_subset1()
        return
  
  def get_file():
  
        start_file='thefile.csv'
  
        with open(start_file, 'rb') as f:
          data = list(csv.reader(f))
          import collections
          counter = collections.defaultdict(int)
  
        for row in data:
          counter[row[10]] += 1
        return
  
  def samples_subset1():
  
        with open('/pythonwork/samples_subset1.csv', 'wb') as outfile:
            writer = csv.writer(outfile)
            sample_cutoff=5000
            b_counter=0
            global b
            b=[]
            for row in data:
                if counter[row[10]] &gt;= sample_cutoff:
                   global b
                   b.append(row) 
                   writer.writerow(row)
                   #print b[b_counter]
                   b_counter+=1
        return
  </code></pre>
  
  <p>i am a beginner at python. the way my code runs is i call do_work and do_Work will call the other functions. here are my questions:</p>
  
  <ol>
  <li><p>if i need <code>data</code>to be seen by only 2 functions should i make it global? if not then how should i call <code>samples_subset1</code>? should i call it from <code>get_file</code> or from <code>do_work</code>?</p></li>
  <li><p>the code works but can you please point other good/bad things about the way it is written?</p></li>
  <li><p>i am processing a csv file and there are multiple steps. i am breaking down the steps into different functions like <code>get_file</code>, <code>samples_subset1</code>, and there are more that i will add. should i continue to do it the way i am doing it right now here i call each individual function from <code>do_work</code>?</p></li>
  </ol>
  
  <p>here is the new code, according to one of the answers below:</p>
  
  <pre><code>import csv
  import collections
  
  def do_work():
        global b
        (data,counter)=get_file('thefile.csv')
        samples_subset1(data, counter,'/pythonwork/samples_subset1.csv')
        return
  
  def get_file(start_file):
  
          with open(start_file, 'rb') as f:
          global data
          data = list(csv.reader(f))
          counter = collections.defaultdict(int)
  
        for row in data:
          counter[row[10]] += 1
        return (data,counter)
  
  def samples_subset1(data,counter,output_file):
  
        with open(output_file, 'wb') as outfile:
            writer = csv.writer(outfile)
            sample_cutoff=5000
            b_counter=0
            global b
            b=[]
            for row in data:
                if counter[row[10]] &gt;= sample_cutoff:
                   global b
                   b.append(row) 
                   writer.writerow(row)
                   #print b[b_counter]
                   b_counter+=1
        return
  </code></pre>