global

how can I control three different threads using threading in python?

How can I control three different threads using threading in Python? Question: I have thread1, thread2 and thread3, global variable x and three different functions to increment x, import threading import time #check = threading.Condition() x=1 def add_by1(): global x x+=1 time.sleep(1) print(x) def add_by2(): x+=2 time.sleep(1) print(x) def add_by3(): x+=3 time.sleep(1) print(x) if __name__==__main__: …

Total answers: 1

Giving different names to df using a for loop

Giving different names to df using a for loop Question: I have a df in python with different cities. I am trying to create a df for each city. So wrote this code in python and it works. It does what I need. But i was wondering if there is any over way to create …

Total answers: 1

NameError: name 'rank' is not defined even though i have mentioned it in global

NameError: name 'rank' is not defined even though i have mentioned it in global Question: The code below is a simplified version of a bigger code. The main action i want to perform is to use and change rank and progress inside the inc_progress(self) function. class User: rank=-8 progress=0 def inc_progress(self): global rank, progress rank+=1 …

Total answers: 1

Dundered global variable cannot be accessed inside a class method

Dundered global variable cannot be accessed inside a class method Question: I am experiencing an obscure (to me) effect of dundered scoping, and trying to work out the rule for it: #!/usr/bin/env python3 stuff = "the things" __MORE_STUFF = "even more" class Thing: def __init__(self): global __MORE_STUFF # doesn’t help print(stuff) # is OK print(__MORE_STUFF) …

Total answers: 1

UnboundLocalError: local variable <function> referenced before assignment

UnboundLocalError: local variable <function> referenced before assignment Question: I have read similar questions on this topic such as from here, here, and here, and others on scope, but I still cannot understand why answers do not explain these test cases. First see this: def take_sum(a, b, c): return a+b+c def main(): print("take_sum" in globals()) print(take_sum(1,2,3)) …

Total answers: 2

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

Python – Easiest way to define multiple global variables

Python – Easiest way to define multiple global variables Question: I am trying to look for an easy way to define multiple global variables from inside a function or class. The obvious option is to do the following: global a,b,c,d,e,f,g…… a=1 b=2 c=3 d=4 ….. This is a simplified example but essentially I need to …

Total answers: 5

How to implement a Global Python Logger?

How to implement a Global Python Logger? Question: How can I implement a global logger for all of my python files? Some relevant SE questions are one and two, but neither do exactly what I want, simply. I want the log file output to be seen in the console as well. Asked By: xinthose || …

Total answers: 1

Python: modify method-local variable inside inner method

Python: modify method-local variable inside inner method Question: I’m writing a test method called, say, test_foo (using pytest). I am testing the behavior of a function, foo, which takes as an argument another function, get. foo calls get iteratively, conditionally on the return value of get, e.g.: def foo(get, param): max_num_tries = 3 curr_num_tries = …

Total answers: 1

UnboundLocalError when variable is clearly local

UnboundLocalError when variable is clearly local Question: I have a program similar to this: curpuz = 1 def puzzle(req,_): puzzledat = json.loads(open(‘puzzles.txt’,’r’).read()) puzzleque = puzzledat[str(curpuz)][‘q’] req.say(‘Current puzzle: ‘ + puzzleque + ‘ – !ans <answer>’) def puzzlea(req,arg): puzzledat = json.loads(open(‘puzzles.txt’,’r’).read()) puzzleans = puzzledat[str(curpuz)][‘a’] if arg[0] == puzzleans: req.say(‘%tip ‘ + req.nick + ‘ 50 — …

Total answers: 1