global-variables

Access to global variables from a module in Python?

Access to global variables from a module in Python? Question: let’s start with a basic code that uses a global variable : def fun(): print(gvar) gvar = 3 fun() that of course prints “3” in the console. Then, I move my function “fun” in a module “mymod”, and do : from mymod import * gvar=3 …

Total answers: 3

How to store Global variable in Django view

How to store Global variable in Django view Question: In Django views, is it possible to create a global / session variable and assign some value (say, sent through an Ajax call) in a view and make use of it in another view? The scenario I’m trying to implement will be something like: View view1 …

Total answers: 4

How to avoid using global variables?

How to avoid using global variables? Question: I use global variables but I’ve read that they aren’t a good practice or pythonic. I often use functions that give as a result many yes/no variables that I need to use in the main function. For example, how can I write the following code without using global …

Total answers: 3

Override globals in function imported from another module

Override globals in function imported from another module Question: Let’s say I have two modules: a.py value = 3 def x() return value b.py from a import x value = 4 My goal is to use the functionality of a.x in b, but change the value returned by the function. Specifically, value will be looked …

Total answers: 3

globals() scope inside a function

globals() scope inside a function Question: I have a question regarding globals() in python My sample code b=9 def a1(): ‘kkk’ a1() print globals() I got output b as global Since b is global, I am expecting I can modify it anywhere So I modified my code to b=9 def a1(): ‘kkk’ b=100 a1() print …

Total answers: 3

'Global name not defined' concept in python

'Global name not defined' concept in python Question: I am learning Python and have read blogs about this error, but I am still not able to understand this clearly yet. This is a snippet of the code I am writing: for i in included: global signs,accounts, regions global sign_name, acc_name, rg_name if type == “regions”: …

Total answers: 4

Disable global variable lookup in Python

Disable global variable lookup in Python Question: In short, the question: Is there a way to prevent Python from looking up variables outside the current scope? Details: Python looks for variable definitions in outer scopes if they are not defined in the current scope. Thus, code like this is liable to break when not being …

Total answers: 6

Python: modify global var from a function argument

Python: modify global var from a function argument Question: I’d like to create a function that will modify an initialized global variable based on the argument passed to it, but I get a SyntaxError: name ‘arg’ is local and global. I have seen other methods to accomplish this, using globals() or creating a simple func …

Total answers: 3

Using a global variable with a thread

Using a global variable with a thread Question: How do I share a global variable with thread? My Python code example is: from threading import Thread import time a = 0 #global variable def thread1(threadname): #read variable “a” modify by thread 2 def thread2(threadname): while 1: a += 1 time.sleep(1) thread1 = Thread( target=thread1, args=(“Thread-1”, …

Total answers: 5

Why are global variables evil?

Why are global variables evil? Question: I’m trying to find out why the use of global is considered to be bad practice in python (and in programming in general). Can somebody explain? Links with more info would also be appreciated. Asked By: LarsVegas || Source Answers: This has nothing to do with Python; global variables …

Total answers: 4