This question already has answers here:
Using global variables between files?
(9 answers)
Closed 2 years ago.
I would like to know please, how can I define variables in a python file and share these variables with their values with multiple python files?
To do this, you can create a new module specifically for storing all the global variables your application might need. For this you can create a function that will initialize any of these globals with a default value, you only need to call this function once from your main class, then you can import the globals file from any other class and use those globals as needed.
You can create a python module
Create a py file inside that module define variables and import that module in the required places.
Related
This question already has answers here:
Why is globals() a function in Python?
(1 answer)
Reason for globals() in Python?
(9 answers)
Closed 2 days ago.
I'm tracing a legacy project, and see the source code is doing this:
globals()[f'{object_name}s']
I know we use global keyword when we want to change the value for the global variable in a local scope.
But what does global() do?
You can use the python interperter or pydoc to help you answer questions about keywords
$> pydoc globals
output:
Help on built-in function globals in module __builtin__:
globals(...)
globals() -> dictionary
Return the dictionary containing the current scope's global variables.
(END)
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.
This question already has answers here:
python - should I use static methods or top-level functions
(5 answers)
Closed 6 years ago.
I need something to organize some utilty functions.
Variant 1.
create module and write all functions in this module.
Variant 2.
(since python has no staticclass)
create class with only static methods in main module
In general, there is no big difference.Like :
var 1)
import functionsmodule
workers = functionmodule.get_all_workers(**kwargs)
var 2)
workers = FunctionClass.get_all_workers(**kwargs)
I like the second one though.
Questions is : what is best way to do such organization ?
You could also do:
from functionsmodule import get_all_workers, some_other_method
workers = get_all_workers(**kwargs)
After a couple of months learning python, this is my preferred solution. It cleans up the code without the need to reference a class or module. I'd only recommend this if your utilities method names WONT clash with builtins or other functions
I'd usually organise these re-usable/utilities within a common package and implement something like:
from common.functions import get_all_workers, some_other_method
workers = get_all_workers(**kwargs)
EDIT: Based on your "get_all_workers" method name - I'd imagine this should go into some sort of persistence/worker class rather than a general utilities class
This question already has answers here:
Python function pointer
(8 answers)
Closed 9 years ago.
if I have a string like 'module.function', How can I execute function just by one step?
likesomefunction('os.error','args')
You can dynamically get the modules using sys.modules and then you can use getattr to get the attributes from the module, like this
import sys
func = "os.error"
module, function = func.split(".", 1)
getattr(sys.modules[module], function)()
sys.modules can give only the modules which are already loaded. So, if you want to load a module dynamically you can use __import__ function like this
For example,
module, function = "math.factorial".split(".", 1)
print getattr(__import__(module), function)(5)
Output
120
All you need to do is
from module import function
and you'll be able to call
function(x, y, z)
in your code.
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.