global-variables

"Name 'x' is parameter and global" error in Python

"Name 'x' is parameter and global" error in Python Question: I was wondering why this won’t work? I’m fairly new to programming and I’m learning Python. def convert(x,y): while True: try: global x x = int(input(“Number: “)) except ValueError: print(“Make sure it is a number.”) while True: try: global y y = int(input(“Number: “)) except …

Total answers: 6

python global variable inside a module

python global variable inside a module Question: a.py contains: ip=raw_input() import b show() b.py contains: def show: global ip print ip it’s showing an error saying that ip is not defined. how can i use a variable from the main script inside a module? Asked By: RatDon || Source Answers: The way your code is …

Total answers: 3

Global variable with Django and Celery

Global variable with Django and Celery Question: I have a code like this, wl_data = {} def set_wl_data(): global wl_data wl_data = get_watchlist_data() def get_wl_data(scripcodes): # Filtering Data result = {scripcode:detail for scripcode, detail in wl_data.iteritems() if int(scripcode) in scripcodes or scripcode in scripcodes} return result I am running this as a django project, I …

Total answers: 2

Python global keyword vs. Pylint W0603

Python global keyword vs. Pylint W0603 Question: Pylint W0603 states: Using the global statement. Used when you use the “global” statement to update a global variable. PyLint just try to discourage this usage. That doesn’t mean you can not use it ! I wonder why is it so? Is there any more Pythonic way of …

Total answers: 4

Global dictionaries don't need keyword global to modify them?

Global dictionaries don't need keyword global to modify them? Question: I wonder why I can change global dictionary without global keyword? Why it’s mandatory for other types? Is there any logic behind this? E.g. code: #!/usr/bin/env python3 stringvar = “mod” dictvar = {‘key1’: 1, ‘key2’: 2} def foo(): dictvar[‘key1’] += 1 def bar(): stringvar = …

Total answers: 2

How to make a local variable (inside a function) global

How to make a local variable (inside a function) global Question: I’m using functions so that my program won’t be a mess but I don’t know how to make a local variable into global. Asked By: user1396297 || Source Answers: Simply declare your variable outside any function: globalValue = 1 def f(x): print(globalValue + x) …

Total answers: 5

Is it possible to define global variables in a function in Python

Is it possible to define global variables in a function in Python Question: How do I declare a global variable in a function in Python? That is, so that it doesn’t have to be declared before but can be used outside of the function. Asked By: WoooHaaaa || Source Answers: Yes, but why? def a(): …

Total answers: 2

Reason for globals() in Python?

Reason for globals() in Python? Question: What is the reason of having globals() function in Python? It only returns dictionary of global variables, which are already global, so they can be used anywhere… I’m asking only out of curiosity, trying to learn python. def F(): global x x = 1 def G(): print(globals()[“x”]) #will return …

Total answers: 10

Function not changing global variable

Function not changing global variable Question: my code is as follow: done = False def function(): for loop: code if not comply: done = True #let’s say that the code enters this if-statement while done == False: function() For some reason when my code enters the if statement, it doesn’t exit the while loop after …

Total answers: 4

Insert variable into global namespace from within a function?

Insert variable into global namespace from within a function? Question: Is it possible to add an object to the global namespace, for example, by using globals() or dir()? def insert_into_global_namespace(var_name, value): globals()[var_name] = value insert_into_global_namespace(‘my_obj’, ‘an object’) print(f’my_obj = {my_obj}’) But this only works in the current module. Asked By: Dan || Source Answers: Yes, …

Total answers: 5