Am i making a mistake using global variables like this? [duplicate] - python

This question already has answers here:
How do I use global variables in python functions? [duplicate]
(7 answers)
Closed 5 years ago.
Im trying to use global variables in order to declare them at the start of my code just like you would in C# however when ever i edit them in a function and i try call it in another function it throws an error saying that the variable is not declared?
This is where i declare the variables:
from tkinter import *
import os
global Name
global Wmain
global directory
global Username
global Password
global Code
This is where i change the the directory variable:
def NameGet():
Name = NameEntry.get()
directory = ('C:\\Users\\Bradley\\Desktop\\Log In system\\Members\\' + Name + '.txt')
CheckFile(Name)
This is where i am getting the error:
def SignUpFinished():
with open(directory, W) as F:
F.write(Username)
F.write(Password)
F.write(Code)
F.close()
Now i feel im either making a really novice mistake or something isnt working right with my code. any ideas?

In order to use global variable you need to explicitly set it inside a method.
For example:
a=4
def func():
global a
print(a)
func()

Related

Difference between global/local variables in Python [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 4 months ago.
test=4
def sum1(var):
print("in function, test",test)
var+=1
test=7
print("in function, var",var)
print("in function, test",test)
sum1(5)
print(test)
I'm trying to understand global and local variable, so I try it this way: a global and a local variable that has same names (I know we normally don't do that).
In this program it has an "UnboundLocalError: local variable 'test' referenced before assignment"
I am wondering why the first print() that prints the test cannot be printed out? Why wouldn't it call the global "test"?
Because your local test shadows the name of the global test, it's unclear to which one you're referring. If you comment out the line test = 7 in your function, you won't get the error, as python assumes you're referring to the global test.
If you insist on using var names in your local scope that shadow global vars (and you've just discovered why that's a very bad idea), you can be explicit about it:
def sum1(var):
global test
print("in function, test", test)
...
Now, however, when you get to test = 7, you're modifying the global test var. That's probably not what you wanted.

Python: Dynamically access variable from another file [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 1 year ago.
I want to access the python variable from config file dynamically like below.
config file:
total_var_count = 4
var1=1
var2=2
var3=3
var4=4
main_file.py
import config as cf
for variable_no in range(1,int(cf.total_var_count)+1):
print(cf.var+str(variable_no))
another method I have tried:
new_var ='var'+str(1)
print(cf.new_var) # which is also not working.
I have tried the above methods to access but its not working. Could someone tell me how I can access the variable from config file dynamically?
You can use getattr() for this:
main_file.py
import config as cf
new_var = 'var' + str(1)
print(getattr(cf, new_var))
The attribute is here the variable of the imported module.

Python 3 and global variable in module [duplicate]

This question already has answers here:
Visibility of global variables in imported modules
(9 answers)
Closed 3 years ago.
main1.py
import mya
a=10
mya.increment_a()
a=a-5
print(a)
module mya.py
def increment_a():
global a
a=a+1
print(a)
name 'a' is not defined.
I don't understand why. I declare variable a as global in module, so from this point a=0 as it is in mail1.py
upd: I need work globaly. Starting value for variable "a" set in main.py, function in module mya.py will edit "a", and return new value to main.py for further use.
--- closed topic---
Now I use "arguments" and "return" and in work:
mya.py
def increment_a(a):
a=a+1
print(a)
return a
main1.py
import mya
a=0
print(a)
a=mya.increment_a(a)
a=a+10
print(a)
From Visibility of global variables in imported modules:
Globals in Python are global to a module, not across all modules. (Many people are confused by this, because in, say, C, a global is the same across all implementation files unless you explicitly make it static.)
One approach to deal with it is:
import mya
mya.a=3
mya.increment_a()

Assigning values to variables in separate scripts with Python and Pygame [duplicate]

This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 8 years ago.
I need to create a variable in a different script from the main one in my game I am working on, with Python and Pygame.
For example:
def test():
a = 10
def testing():
return a
Then I run code like this:
import (script name)
script name.test()
script name.testing()
And after this, it gives an error. How can I fix this problem?
'a' in testing() is not a global variable and hence it's not recognised from previous function test(). If you really want to use 'a' from test() then you can probably define 'a' as Global Variable.

Python - how do I write a "getter" function that will return a variable stored in my module (initiating it first if needed)? [duplicate]

This question already has answers here:
Using global variables in a function
(25 answers)
Closed 6 months ago.
Here is the complete code of my module, called util.py:
import my_other_module
__IMPORTANT_OBJECT__ = None
def getImportantObject():
if __IMPORTANT_OBJECT__ is None:
__IMPORTANT_OBJECT__ = my_other_module.ImportantObject()
return __IMPORTANT_OBJECT__
My understanding is that variables prefixed with a double underscore are considered private to a module. The idea here is that I would like to store a private reference to the important object and the return it to anyone who asks for it via the getImportantObject() method. But I don't want the object to be initiated until the first time this method is called.
When I run my code, however, I get the following error:
File "/Users/Jon/dev/util.py", line 6, in getImportantObject
if __IMPORTANT_OBJECT__ is None:
UnboundLocalError: local variable '__IMPORTANT_OBJECT__' referenced before assignment
What is the recommended way to accomplish what I am trying to do here?
The variable is not considered private; rather it's seen as a local variable.
Use the global keyword to mark it as such:
def getImportantObject():
global __IMPORTANT_OBJECT__
if __IMPORTANT_OBJECT__ is None:
__IMPORTANT_OBJECT__ = my_other_module.ImportantObject()
return __IMPORTANT_OBJECT__

Categories

Resources