Call a variable outside of a function? [duplicate] - python

This question already has answers here:
Using global variables in a function
(25 answers)
Closed 3 years ago.
For example:
def test(a)
a = 2
b = 5+a
x = a*2
return x
Is there a way to call the variable 'b' outside of the function?

b = None
def test(a)
global b
a = 2
b = 5+a
x = a*2
return x
print(b)
Although we will suggest to return b and then access it, as global variable is a bad idea.
See this,
Why are global variables evil?

Related

Why some variables have changed and some have not? [duplicate]

This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 3 months ago.
Why the variables a, c, d have not changed, but b has changed?
a = 0
b = []
c = []
d = 'a'
def func_a(a):
a += 1
def func_b(b):
b += [1]
def func_c(c):
c = [2]
def func_d(d):
d += 'd'
func_a(a)
func_b(b)
func_c(c)
func_d(d)
print('a = ', a)
print('b = ', b)
print('c = ', c)
print('d = ', d)
I think it has to do with the fact that all variables are global, but I don't understand why b changes then..
This is related by local and global scope, you can update using change the name of function parameter names;
a = 0
def func_a(local_a):
global a
a += 1
func_a(a)
print('a = ', a)
# output: a = 1
"global a" meaning this function will use the global scope of a.
If you try to use with this way;
a = 0
b = []
c = []
d = 'a'
def func_a(a):
global a
a += 1
func_a(a)
print('a = ', a)
You will get an error like this;
File "glob_and_local.py", line 8
global a
^^^^^^^^
SyntaxError: name 'a' is parameter and global
Because same names are conflicting.
Here, a, b, c, d are global variables.
For a:
a is an integer. when you are calling func_a(a) you are only making changes in the local scope but not returning anything. That's why there is no change in the global variable a.
Same thing happening with global variables c and d.
For b:
You are passing an array and appending an array into it.
append method makes changes in global variables too.
NOTE:
here, b += [1] is equivalent to b.append(1)

My variables updated in a function don't work outside the function [duplicate]

This question already has answers here:
Python function global variables? [duplicate]
(6 answers)
Function not changing global variable
(4 answers)
Closed 1 year ago.
hi = 1
result = 0
def hello(x):
x = x + 10
result = x
helli(hi)
print(result)
Why does my code Output "0" and not 11?
You need to use the keyword global for that. Then edited values inside the function actually impact the variable from outside the function. This is not the recommended approach.
hi = 1
result = 0
def hello(x):
global result
x = x + 10
result = x
hello(hi)
print(result)
The recommended approach is, however, to return such value:
def hello(x):
return x + 10
hi = 1
result = 0
result = hello(hi)
print(result)

Why is the output not 1 and 1? [duplicate]

This question already has an answer here:
How do the scoping rules work with classes?
(1 answer)
Closed 2 years ago.
a = 0
b = 0
def test():
a = 1
b = 1
class Test:
print(a, b)
a = 2
test()
It gives
0 1
It should be
1 1
Why is this happening?
Inside the test function, you're not actually initializing a new Test object, so Python will read through the class definition to use it inside the function scope, so it does execute the print statement. The problem is that you are trying to change the value of a in the class scope and this generates that a take the previous value of a outside the function definition.

Is there a way to change a global variable in a function (like as a counter) and then have it keep that value for use in another function? [duplicate]

This question already has answers here:
Using global variables in a function
(25 answers)
Closed 2 years ago.
For ex if a is my global var:
a = 0
def func1():
#something happens
a += 1
return a
(repeats 2 more times)
def func2():
if a == 3:
#do something
Would this work? I apologize if this is unclear, as I am very new to Python.
This is the return doing,
use return to reuse a result.
a = 0
def add1(a):
a += 1
return a
a = add1(a)
def func1(num):
if num == 3:
...
func1(a)

How should I code a function to turn a parameter I input into 1? [duplicate]

This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 8 years ago.
I am the new to Python. Here is a problem that I have encountered.
Assume that there is a function called set1(x).
Here is the code:
def set1(x):
x = 1;
When I run this,
m = 5
set1(m)
m
the value of m is still 5.
So how should I code the function if I want the parameter to become 1 whenever I call the set1() function?
You can return the value
def set1():
return 1;
And call it as
m=5
m = set1()
print (m)
It will print 1
Another way but bad method is to make it global
def set1():
global m
m = 1
Functions use a local namespace in which they declare their variables, which means that in most cases when you pass an argument to a function, it will not effect the argument outside of the function (in the global namespace). So while the value changes within the function, it does not change outside of the function.
If you want the function to change the value, you need to return the value as such:
def set1(x):
x=1
return x
>>> m=5
>>> m = set1(m)
>>> m
1
This said, if your argument is mutable, it can change.
def set2(x):
x[0] = 1
>>> m = [2]
>>> set2(m)
>>> m[0]
1

Categories

Resources