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)
Related
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.
This question already has answers here:
Visibility of global variables in imported modules
(9 answers)
Closed 3 years ago.
main1.py
import mya
a=10
mya.increment_a()
a=a-5
print(a)
module mya.py
def increment_a():
global a
a=a+1
print(a)
name 'a' is not defined.
I don't understand why. I declare variable a as global in module, so from this point a=0 as it is in mail1.py
upd: I need work globaly. Starting value for variable "a" set in main.py, function in module mya.py will edit "a", and return new value to main.py for further use.
--- closed topic---
Now I use "arguments" and "return" and in work:
mya.py
def increment_a(a):
a=a+1
print(a)
return a
main1.py
import mya
a=0
print(a)
a=mya.increment_a(a)
a=a+10
print(a)
From Visibility of global variables in imported modules:
Globals in Python are global to a module, not across all modules. (Many people are confused by this, because in, say, C, a global is the same across all implementation files unless you explicitly make it static.)
One approach to deal with it is:
import mya
mya.a=3
mya.increment_a()
This question already has answers here:
Finding the source code for built-in Python functions?
(8 answers)
Closed 6 years ago.
I am searching through method/class declarations in Pycharm and I made it down to __builtin__.py, but there does not appear to be any code written in this module.
Here is the snippet that I came across:
def hasattr(p_object, name): # real signature unknown; restored from __doc__
"""
hasattr(object, name) -> bool
Return whether the object has an attribute with the given name.
(This is done by calling getattr(object, name) and catching exceptions.)
"""
return False
Where is the code that is actually executed when I call hasattr()? Is the line # real signature unknown; restored from __doc__ a clue as to why there is no code here?
I am not interested in changing anything. I am simply surprised that there is no code written here.
They are built in functions.
It means that they are a direct calls to methods embedded in python binaries. Simply, the python code for them doesn't exist.
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.