-1

I have defined some global parameters in Python (that should also be accesible from other files) and I would like to change them inside the class in which they are defined. I read quite often that one way of doing this is to use 'global' when defining the variables inside the function. However, I also read quite often that 'global' should be avoided basically as it is not good style. Now my question is what other opportunities do I have to change my variables? When just passing them to the function as arguments (this was also one suggestion) their values remain the same.

So here is my code:

from random import random

numberOfBuildings = 10
numberOfVehicles = 10
useMonteCarloMethodForScenarioCreation = True

def monteCarloScenarioGeneration(numberOfBuildings, numberOfVehicles):
    numberOfBuildings_MonteCarlo  = int(  numberOfBuildings *random() *2  + 1)
    numberOfVehicles_MonteCarlo  = int(numberOfVehicles *random() *2  + 1)
    
    if useMonteCarloMethodForScenarioCreation ==True:
        numberOfBuildings = numberOfBuildings_MonteCarlo
        numberOfVehicles = numberOfVehicles_MonteCarlo

    

monteCarloScenarioGeneration(numberOfBuildings, numberOfVehicles)

    
print('numberOfBuildings: ', numberOfBuildings)
print('numberOfVehicles: ', numberOfVehicles)

The value of the global variables numberOfBuildings and numberOfVehicles are not changed when the function monteCarloScenarioGeneration is called. They keep the same values that they have after being initialized (10). How can I change the global variables without using the keyword global? I'd appreciate any comments.

PeterBe
  • 700
  • 1
  • 17
  • 37
  • you can use class with attributes. You can use main section and define variables there. You can do it on many ways – Wonka Mar 19 '21 at 11:15
  • Thanks Wonka for your comment. How can I do this without creating a separate class for that? – PeterBe Mar 19 '21 at 11:21
  • 1
    By definition what you are asking for is impossible to do in Python. Reassignments of any names residing in the global scope of the current module without using the `global` keyword is not possible. This is not to say they can't be mutated, e.g. you may instead use a `dict` to store values, e.g. declare `numbers = {}` and then inside functions do something like `numbers['OfBuildings'] = 10` and any mutations to `numbers` like so will be reflected. – metatoaster Mar 19 '21 at 11:24
  • For more information refer to [this thread](https://stackoverflow.com/questions/14081308/why-is-the-global-keyword-not-required-in-this-case). – metatoaster Mar 19 '21 at 11:27
  • Thanks metatoaster for your answer. I do not want to use a dictionary because I just want to have a variable that should be changed inside a function. So this means I have to use global altough many think it is bad to use global? – PeterBe Mar 19 '21 at 11:29

1 Answers1

2

You should just be able to use return to change the variables outside of the function, if I'm reading this right.

As the last line of the function, put

return numberOfBuildings, numberOfVehicles

and then when calling the function

numberOfBuildings, numberOfVehicles = monteCarloScenarioGeneration(numberOfBuildings, numberOfVehicles)