title: Should I use a global variable or is there any other option in this example?
body: <p>I have a question regarding the use of global variable. I have a situation something like this: I have to loop through forest and then through hives to find the bee and then the bee should deposit honey in honey_jar.</p>
  <p>I could use this approach:</p>
  <pre><code>class HoneyJar:
      def deposit_honey(self, bee):
          print(f&quot;Honey desposited by {bee}&quot;)
  
  
  def loop_bees(nest):
      bees = [1, 2, 3, 4]  # Assume i have bees from some nest
      for bee in bees:
          honey_jar = HoneyJar()
          honey_jar.deposit_honey(bee)
  
  
  def loop_hives(forest):
      bee_nests = [1, 2, 3, 4]  # Assume i have bee_nest from some forest
      for bee_nest in bee_nests:
          loop_bees(bee_nest)
  
  
  def main():
      forests = ['Amazon', 'Madagascar']
      for forest in forests:
          loop_hives(forest)
  
  
  main()
  </code></pre>
  <p>But that would mean I create instance of HoneyJar in every loop.</p>
  <p>I could tackle this problem by creating the honey_jar on top level and using it as a global variable.</p>
  <pre><code>honey_jar = HoneyJar()
  
  def loop_bees(nest):
      global honey_jar
  
      bees = [1, 2, 3, 4]  # Assume I have bees from some nest
      for bee in bees:
          honey_jar.deposit_honey(bee)
  </code></pre>
  <p>What would a better approach?</p>