-2

In Python, I was wondering something about defining a variable in a module which can be used by all functions in that module.

I know that if I define a variable outside function, I can access that variable in a function by introducing global

e.g : inside a module name gas.py

R = 8.314 # univsersal gas constant

def pressure(T, V, n):
    global R
    P = n*R*T/V
    return P

def temperature(P,V,n):
    global R
    T = P*V/(R*n)
    return T

But as you can see, I have to write global R inside each function.

Is there a way that I can access R without writing global R inside each function ? For example :

R = 8.314 # univsersal gas constant

def pressure(T, V, n):
    P = n*R*T/V
    return P

def temperature(P,V,n):
    T = P*V/(R*n)
    return T

Thank you

Jonses
  • 57
  • 8
  • Sorry it's a mistake... it's correct now – Jonses Dec 17 '20 at 12:04
  • 2
    `global` is only needed to assign to a global, not to read it. – user2357112 Dec 17 '20 at 12:05
  • 1
    Well did you try to run the second version? It actually works... – Tomerikoo Dec 17 '20 at 12:12
  • " I can access that variable in a function by introducing global" No, using `global` **is not needed to access a variable**. It is only needed to *assign to global variable in a non global scope* Your example *already* can do this. Did you even try? – juanpa.arrivillaga Dec 17 '20 at 12:13
  • I tried on the console Python, not inside the module, just for testing. It didn't work, so I panicked a bit thinking only global worked. But now after looking again to my script in the console I think I wrote wrongly – Jonses Dec 17 '20 at 12:18

2 Answers2

0

In python, you can read the value of a global variable without declaring it global.
You only need the global declaration when you want to change the value of a global variable. So in your case, you can just delete the all global declarations.

Roy Cohen
  • 1,540
  • 1
  • 5
  • 22
-2

No, in Python a global variables works in a different way compared to other programming languages.

If you want to access a variable contained outside a function, you can just call it like a regular variable, for example:

word = "hello"

def function():
    print(word)

function()

Output: hello

If you want to edit a variable locally, but not globally, you have to reassign the variable inside the function, for example:

word = "hello"

def function():
    word = "world"
    print(word)

print(word)

Output: hello, because we reassigned the variable only inside the function, so the global value of the variable word is still hello

But, if we want to edit a global variable (such as in case 1) in a non-global scope (local scope) and we want to manipulate the value of that variable inside a function, we have to use the global declaration, for example:

word = "hello"

def function():
    global word 
    word = "world"
    
function()
print(word)

Now the Output will be world.

Markintosh
  • 44
  • 7
  • That's incorrect, python **do** have global variables. – Roy Cohen Dec 17 '20 at 12:11
  • @RoyCohen mmm not "true" global, Python global variables are global to the *module*. If you have a global variable in a module, you cannot access it in another module. Now, you can "cheat" and use the `__builtins__` namespace to emualte "true" global variables but that is hackey – juanpa.arrivillaga Dec 17 '20 at 12:15
  • In any case, I don't think that is what the OP is asking about... – juanpa.arrivillaga Dec 17 '20 at 12:15
  • 1
    You can access global variables of one module from another by simply doing `module_name.global_variable_name`, for example `math.pi`. – Roy Cohen Dec 17 '20 at 12:17
  • Sorry, I recognize that my answer was not completely clear. Anyways, the OP just asked a simple question and I gave him a simple answer. – Markintosh Dec 17 '20 at 12:24
  • @RoyCohen right, of course, but in other languages, global variables don't require that. That is the distinction I'm making. They work like names in `__builtins__`, e.g. you can access `list` *anywhere* without having to explicitly use the namespace. That is often what people mean by "python doesn't have global variables" – juanpa.arrivillaga Dec 17 '20 at 12:24
  • In python, those kind of variables are called "global variables" so saying that python doesn't have global variables is incorrect, even if other languages have *globaler* variables. – Roy Cohen Dec 17 '20 at 12:28