Accessing variable in a function without calling it in Python - python

Here is my code
def function():
global x
x = 5
print (x)
How can I access the local variable x inside the function without calling the function itself?

It's not possible. A variable inside a function will only be after the function is called, else it is non-existent. Look into how locallocal,non-local, and global variables work.
def function():
global x
x = 5
print(x)
function()
else you should just put x outside the function().

Just put x = 5 outside the function

Related

How to change the global value of a parameter when using a function in python

This is what I want to achieve
Variable=0
Some_function(Variable)
print (Variable)
I want the output to be 1 (or anything else but 0)
I tried using global by defining some_function like this, but it gave me an error "name 'Variable' is parameter and global"
def Some_function(Variable):
x=Variable+1
global Variable
Variable=x
You are using Variable as your global variable and as function parameter.
Try:
def Some_function(Var):
x=Var+1
global Variable
Variable=x
Variable=0
Some_function(Variable)
print (Variable)
You should not use the same name for parameter and the globale variable
The error message seem clear enough. Also, you wouldn't need to pass Variable as a parameter if you are using a global.
If you define
def f():
global x
x += 1
Then the following script should not output an error :
x = 1 # global
f(x)
print(x) # outputs 2
Another possibility :
def f(y):
return y+1
Which you can use like this :
x = 1
x = f(x)
print(x) # 2
If you want to modify global variable, you should not use that name as function parameter.
var = 0
def some_func():
global var
var += 1
some_func()
print(var)
Just use global keyword and modify variable you like.
Variable = 0
def some_function(Variable):
global x
x = Variable + 1
some_function(Variable)
Variable = x
print(Variable)

the show() function gives me 15 & I understand but why does the print(x) function return 13 I don't get it I am new to python can you pls help me

I was trying to understand global and local variables in python and tried out the below-mentioned code but for some reason, the show() statement runs fine and I understand that but the print(x) statement below the show() prints 13 while I was expecting it to print 15
def show():
x=10
x+=5
print(x)
show()
print(x)
That print statement outside the function should not work at all. If you define a variable inside of a function you cant call it outside of it.
Also if your code looks like this:
x = 13
def show():
x = 10
x += 5
print(x)
show()
print(x)
the second print statement will still return 13, because by writing x = 10 you are creating a new variable named x inside the function, which will not be accessible outside this function.
x is probably defined before the function
try to not use the same variable name more than once
assuming x is not defined before this, this code should work
def show():
x = 10
x += 5
return x
show()
print(x)

Python scope when assignment never performed

So I get that
x = 5
def f():
print(x)
f()
print(x)
gives back 5 and 5.
I also get that
x = 5
def f():
x = 7
print(x)
f()
print(x)
gives back 7 and 5.
What is wrong with the following?
x = 5
def f():
if False:
x = 7
print(x)
else:
print(x)
f()
print(x)
I would guess that since the x=7 never happens I should get 5 and 5 again. Instead I get
UnboundLocalError: local variable 'x' referenced before assignment
Does python regard x as a local variable because in this indented block there is an assignment expression regardless if it is executed or not? What is the rule exactly?
When the function is defined python interprets x as a local variable since it's assigned inside the body of the function. During runtime, when you go into the else clause, the interpreter looks for a local variable x, which is unassigned.
If you want both x to refer to the same variable, you can add global x inside the body of the function, before its assignment to essentially tell python when I call x I'll be referring to the global-scope x.
If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations.
You need to use global in your function f() like so:
x = 5
def f():
global x
if False:
x = 7
print(x)
else:
print(x)
f()
print(x)

Why python not printing 100 and 1000 [duplicate]

This question already has answers here:
UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
(14 answers)
Closed 5 years ago.
x=100
def fun2():
print x
x=10000
print x
fun2()
The above program showing local variable x reference before assignment. Why it is not printing
100
10000
x in the function is a local variable and can't access the other local variable you define first because they are in different scope.
Add global x to the start of your function or define x inside the function.
You appear to not know about variable scoping.
The variable x does not exist in the function scope.
You have to place global x before your print statement in order to access the global variable x.
x = 1 # Global x
def f():
x = 2 # function-local x
print(x) # prints 2
f()
print(x) # prints 1 because it uses the global x which remains unchanged
If you want that to work you need to specify inside the function that the x variable you are using is the one in the global scope by using the global keyword.
x=100
def fun2():
# Add this line
global x
print x
x=10000
print x
fun2()
Below code will print the value of x -> 100, as it is there in main scope #samba, but when you change the value of it doesn't work that way as it is not defined in the function.
x = 100
def fun2():
print(x)
fun2()
This doesn't work as the same way:
x = 100
def fun2():
print(x)
x = 1000
print(x)
fun2()
and through error:
UnboundLocalError: local variable 'x' referenced before assignment
x is a local variable and not initialised in function fun2().
You need to understand variable scoping here, Please check Global and Local variable scope
If you want to use it globally use global keyword in your function.
Because u assigned variable before function.
Just try this
def fun2():
x=100
print x
x=10000
print x
fun2()
It will output 100 and 1000

Global Variables in functions with if statements

Okay, I am currently doing a project to make a blackjack game in python and I'm having some trouble. One of my issues is I dont know when to define a variable as global, specifically in functions with if statements. If I have a global variable outside the if statement, do I have to claim that the variable is global within the if statement as well? For example:
x = 5
def add():
global x <--- ?
x += 1
if x == 7:
global x <--- ?
x = 5
I'm pretty sure I need the "global x" at the 1st question mark, but what about at the second question mark? Would I still need to put a "global x" within my if statement if I wanted my if statement to update a global variable? Or does the global x at the beginning of the function make the x's inside the if statement global? Also, if I wanted to return x here, where should I do it?
Only one global statement is enough.
From docs:
The global statement is a declaration which holds for the entire
current code block.
x = 5
def add():
global x
x += 1
if x == 7:
x = 5
Also, if I wanted to return x here, where should I do it?
If you're using global in your function then the return x must come after the global x statement, if you didn't use any global statement and also didn't define any local variable x then you can return x anywhere in the function.
If you've defined a local variable x then return x must come after the definition.

Categories