0

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.

I could use this approach:

class HoneyJar:
    def deposit_honey(self, bee):
        print(f"Honey desposited by {bee}")


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()

But that would mean I create instance of HoneyJar in every loop.

I could tackle this problem by creating the honey_jar on top level and using it as a global variable.

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)

What would a better approach?

Nitish
  • 392
  • 2
  • 7
  • 1
    `loop_bees` should get the needed instance of `HoneyJar` as a parameter. – Matthias Jun 15 '22 at 10:35
  • Now, it's another question whether deposit_honey, should be method of Jar class or Bee class (if you had one). ;-) – buran Jun 15 '22 at 10:40
  • @Matthias but would mean creating instance in each loop of loop_nest, etc. – Nitish Jun 15 '22 at 10:44
  • @buran It doesn''t matter. `honey_jar.deposit_honey(bee)` and `bee.deposit_honey(honey_jar) ` both need instance of HoneyJar from outside. – Nitish Jun 15 '22 at 10:45
  • @Nitish, agree it requires an instance of Jar from outside. And no, you don't need to create the instance in each loop – buran Jun 15 '22 at 10:49

1 Answers1

0

As Matthias suggested you should use something like this in your main.
First thing is to create a single instance of HoneyJar in the main, then pass the reference to the other methods.

def main():
    forests = ['Amazon', 'Madagascar']
    honey_jar = HoneyJar() # instantiate HoneyJar once here
    for forest in forests:
       loop_hives(forest=forest, jar=honey_jar) # then pass it to the other methods

And your loop_hives and loop_bees would be :

def loop_bees(nest, jar):
    bees = [1, 2, 3, 4]  # Assume i have bees from some nest
    for bee in bees:
        jar.deposit_honey(bee)


def loop_hives(forest, jar):
    bee_nests = [1, 2, 3, 4]  # Assume i have bee_nest from some forest
    for bee_nest in bee_nests:
        loop_bees(bee_nest, jar)
Andrei I
  • 11
  • 4
  • Yes that would be the solution but I don’t find it much elegant solution. I have to pass that variable when the function won’t even use it and will just pass to the next function. What if we have other instances to pass just like honey_jar . That would make code a lot cluttered. – Nitish Jun 16 '22 at 04:27
  • It can indeed become cumbersome to pass a whole bunch of arguments to each method. The thing that would really make things more elegant is rearranging the project according to the data. You could have a Hive class that holds a list / dict of bees. The hive method could have a method to deposit_honey_from_all_bees that takes a jar as input. This way you only ever need to pass the jar once. – Andrei I Jun 16 '22 at 08:01