global

global variable warning in python

global variable warning in python Question: I have a python 2.6 script (yes I know I should upgrade to at least 2.7) that looks like this: ret_code = 0 def some_func() global ret_code … if __name__ == ‘__main__’: global ret_code … Now I get a warning if I run the code: *SyntaxWarning: name ‘ret_code’ is …

Total answers: 1

Preserving global state in a flask application

Preserving global state in a flask application Question: I am trying to save a cache dictionary in my flask application. As far as I understand it, the Application Context, in particular the flask.g object should be used for this. Setup: import flask as f app = f.Flask(__name__) Now if I do: with app.app_context(): f.g.foo = …

Total answers: 3

Exceptions for the whole class

Exceptions for the whole class Question: I’m writing a program in Python, and nearly every method im my class is written like this: def someMethod(self): try: #… except someException: #in case of exception, do something here #e.g display a dialog box to inform the user #that he has done something wrong As the class grows, …

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

In Python what is a global statement?

In Python what is a global statement? Question: What is a global statement? And how is it used? I have read Python’s official definition; however, it doesn’t make a lot of sense to me. Asked By: Capurnicus || Source Answers: Basically it tells the interpreter that the variable its given should be modified or assigned …

Total answers: 5

Using global variables between files?

Using global variables between files? Question: I’m bit confused about how the global variables work. I have a large project, with around 50 files, and I need to define global variables for all those files. What I did was define them in my projects main.py file, as following: # ../myproject/main.py # Define global myList global …

Total answers: 9

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

Python RegExp global flag

Python RegExp global flag Question: Is there a flag or some special key in python to use pattern multiple times. I used to test http://gskinner.com/RegExr/ my RegExp, it worked correctly in it. But when testing in correct enviorment match only returns None. import re pattern = r”(?P<date>–dd-w+:dd)[ t]+(?P<user>w+)[ t]+(?P<method>[w ]+)[” ]* (?P<file>[w\:.]+)@@(?P<version>[w\]+)[” ]*(?P<labels>[(w, .)]+){0,1}[s “]*(?P<comment>[w …

Total answers: 2